I have a title that I put into a UILabel. I get it to wrap the text just fine onto two lines, but often the second line just has one small word in it. Is there a way to tell the system to wrap the lines a little more evenly?
The titles are dynamically fetched, so I don't believe I can have this determined ahead of time.
You could figure out what the length would be on a single line, then take that width, divide by 2, and use that result as the width constraint for a 2 line label. That would perhaps get you a better visual result, but you might have to tweak this a little if you have long words that would cause your label to be 3 lines (unless more than 2 lines is ok).
This approach has the disadvantage of doing a little extra work, but it might do the trick. For example:
NSString *text = @"This is my very long title I want evenly on 2 lines"; UILabel *theLabel = [[UILabel alloc] init]; theLabel.text = text; // ...other label setup here, like font, etc [theLabel sizeToFit]; CGSize constraint = CGSizeMake(theLabel.frame.size.width / 2.0, 1000); CGSize labelSize = [theLabel.text sizeWithFont:theLabel.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; theLabel.numberOfLines = 0; theLabel.lineBreakMode = UILineBreakModeWordWrap; CGRect frame = theLabel.frame; frame.size = labelSize; theLabel.frame = frame;
That should do it. Warning: Written from memory. :-)