Is there a way to "top align" a UILabel, that is make text stick to top of label view? As opposed to center aligned vertically, or float in the center of the view, as is the default?
Here is an image of three labels, aligned left, right and center, and a UITextView that is aligned center and top. The text of the textView sticks to the top regardless of the vertical size of the view. Can I do the same thing with a label?
Answer: Select the cells that you wish to align. Right-click and then select "Format Cells" from the popup menu. When the Format Cells window appears, select the Alignment tab. Then select "Top" in the drop-down box called Vertical.
To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.
Build A Paint Program With TKinter and Python Tkinter Label widget can be aligned using the anchor attributes. In order to calculate the accommodate spacing and alignment of the widget, anchor would help in a better way. Anchor provides several options such as N, W, S, E, NW, NE.
There’s no way to set the vertical-align on a UILabel, but you can get the same effect by changing the label's frame. I've made my labels orange so you can see clearly what's happening. You can see the text now move up of frame view. How about a label with longer text? What should we do.
For better results, you can do this StackView > View > UILabel, because when you just embed an UILabel inside a StackView, the StackView resize to fit UILabel to minimum size. H owever, you also have another way that is handle in a subclass of UILabel that will handle vertical alignment for you when setting its alignment property.
What should we do. f you have a label with longer text that will make more than one line, set numberOfLines to 0 (zero here means an unlimited number of lines). Embed Label in StackView and set the following two attributes of StackView in Attribute Inspector:
There's a workaround for it:
constraint
to your UILabel
with a constant <= to the maximum height|width you want.number of lines
to zero.storyboard
to fit only one line.UILabel
call sizeToFit
.This would make your UILabel
to resize within the bounds of the maximum width & height you want and text will always be top aligned.
I used the answer linked above from nevan king in a comment to my question: Vertically align text to top within a UILabel
The code I used is here:
- (void)setUILabelTextWithVerticalAlignTop:(NSString *)theText { CGSize labelSize = CGSizeMake(250, 300); CGSize theStringSize = [theText sizeWithFont:targetLabel.font constrainedToSize:labelSize lineBreakMode:targetLabel.lineBreakMode]; targetLabel.frame = CGRectMake(targetLabel.frame.origin.x, targetLabel.frame.origin.y, theStringSize.width, theStringSize.height); targetLabel.text = theText; } //linked to a button that sets the text from a UITextView to the label. - (IBAction)setText:(id)sender { [sourceText resignFirstResponder]; [self setUILabelTextWithVerticalAlignTop:sourceText.text]; [targetLabel setNumberOfLines:0]; [targetLabel sizeToFit]; }
Here is the project:
owolf.net/uploads/StackOverflow/verticalAlignUILabel.zip
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