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.
Problem is that none of mentioned functions is realiable and for some string and font will return incorrect height value. Especially will fail for attributed texts.
The only realiable solution is here: https://stackoverflow.com/a/4214978/699944 and the point is to use CoreText to manually calculate height of every line to get right size. There is no other known way to do this.
Thanks guys for help, here is the code I tried which is working for me
And for those that are migrating to iOS 8, here is a class extension for Swift:
You can implement
TableViewController's
(UITableViewCell *)tableView:cellForRowAtIndexPath
method in the following way (for example) :Also use
NSString
'ssizeWithFont:constrainedToSize:lineBreakMode:
method to compute the text's height.You were going in the right direction. All you need to do is: