Reputation: 3860
I have a little problem with multiline UILabel, my UILabel text like starts from the middle strangely and it goes up when new lines arrive so that the last line is on the middle always. I want it to behave like a normal textview, starting from top and lines going under each other, first line staying on top. Sorry if I explained it badly, I can try to elaborate if needed! Thanks in advance!
Upvotes: 5
Views: 4898
Reputation: 611
Since sizeWithFont method is decprecated in iOS 7.0 +, you can use a alternative method named boundingRectWithSize.
For Example:
NSDictionary *attrsDictionary =[NSDictionary dictionaryWithObject:YourFont forKey:NSFontAttributeName];
NSAttributedString *attrString =[[NSAttributedString alloc] initWithString:yourString attributes:attrsDictionary];
textRect = [attrString boundingRectWithSize:yourSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
Upvotes: 0
Reputation: 30846
You can use the sizeWithFont:constrainedToSize:lineBreakMode:
method on NSString to figure out the height of a block of text given a font and a constrained width. You would then update the frame of your label to be just large enough to encompass the text.
CGSize textSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(20.0f, 20.0f, textSize.width, textSize.height);
Upvotes: 11