Consider I have the following text in a UILabel
(a long line of dynamic text):
Since the alien army vastly outnumbers the team, players must use the post-apocalyptic world to their advantage, such as seeking cover behind dumpsters, pillars, cars, rubble, and other objects.
I want to resize the UILabel's
height so that the text can fit in. I'm using following properties of UILabel
to make the text within to wrap.
myUILabel.lineBreakMode = UILineBreakModeWordWrap;
myUILabel.numberOfLines = 0;
Please let me know if I'm not heading in the right direction. Thanks.
sizeWithFont constrainedToSize:lineBreakMode:
is the method to use. An example of how to use it is below:Thanks for this post. It helped me a great deal. In my case I am also editing the text in a separate view controller. I noticed that when I use:
in the tableView:cellForRowAtIndexPath: method that the label view was continually rendered over the top of the previous view each time I edited the cell. The text became pixelated, and when something was deleted or changed, the previous version was visible under the new version. Here's how I solved the problem:
No more weird layering. If there is a better way to handle this, Please let me know.
This is one line of code to get the UILabel Height using Objective-c:
and using .height you will get the height of label as follows:
use this here all the properties are used on the label and test it by increasing the text in the itemTitle.text as
it will show the perfetc answer as you need
This method will give perfect height