Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to vertically align text inside labels with a "large" frame

Tags:

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:

enter image description here

like image 696
LuckyLuke Avatar asked Jul 02 '11 13:07

LuckyLuke


People also ask

How do you align text on top of UILabel?

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 .

How do I center a label in Xcode?

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 .


2 Answers

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.

like image 93
EmptyStack Avatar answered May 29 '23 07:05

EmptyStack


I actually noticed that using

[Blue setSizeToFit] 

does align vertically to the top. The default being centered.

like image 45
nembleton Avatar answered May 29 '23 06:05

nembleton