I have a UILabel with 4 lines in a UICollectionView. When there are fewer than 4 rows, I want the text to be on the top and not in the centre.
Thanks..
I have a UILabel with 4 lines in a UICollectionView. When there are fewer than 4 rows, I want the text to be on the top and not in the centre.
Thanks..
this is the solution
dispatch_async(dispatch_get_main_queue(), ^{
yourLabel.numberOfLines = 0;
[yourLabel sizeToFit];
});
An even quicker way to accomplish this is by setting the UILabel's line break mode to "Clip" and adding a fixed amount of newlines.
myLabel.lineBreakMode = NSLineBreakByClipping;
myLabel.text = [displayString stringByAppendingString:@"\n\n\n\n"];
This solution won't work for everyone -- in particular, if you still want to show "..." at the end of your string if it exceeds the number of lines you're showing, you'll need to use one of the longer bits of code -- but for a lot of cases this'll get you what you need.
You should have a look at this blog also.. And this link can also help you Vertically align text to top within a UILabel
In Swift:
yourLabel.lineBreakMode = NSLineBreakMode.ByClipping
yourLabel.text = yourString.stringByAppendingString("\n\n\n\n")