In my application I have multiple tableviews with custom cells. Some of the text in the cells are spread out on between 2-4 lines so the height of the label is large enough to contain the content.
However on iPad the screen is larger and I want the text to appear at the top left point of the label, not in the middle as it do when you use numberOfLines
property. I know you can horizontally align the text, but it must be possible to align the text to the top left also? Or is it impossible.
Screenshot:
Set the UILabel number of lines equal to 0 . Embed the UILabel inside an UIView . Set the UIView constraints (top, right, left and bottom) to occupy the maximum space the UILabel should grow to. Then set the UILabel to top, right, and left of the UIView and also set a constraint to bottom with distance >= 0 .
We want our label to be centrally constrained therefore while it's still selected, hold down the control button and drag a line to the left side of the screen then release it, a pop up menu will appear with this constraint options: Select Center Horizontally in Safe Area .
Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.
As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.
I actually noticed that using
[Blue setSizeToFit]
does align vertically to the top. The default being centered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With