Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top aligned UILabel, make text stick to top of label view -ios

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?

enter image description here

like image 316
Mr Ordinary Avatar asked Aug 08 '12 14:08

Mr Ordinary


People also ask

How do you align text to the top of a cell?

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.

How do I align text to the top in HTML?

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.

How do you align text in a label in Python?

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.

How do I set the vertical-align on a uilabel?

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.

How to align a uilabel in a stackview?

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.

How to make a label with longer text make more than one?

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:


2 Answers

There's a workaround for it:

  1. Set a height & width constraint to your UILabel with a constant <= to the maximum height|width you want.
  2. Set number of lines to zero.
  3. Set the height in storyboard to fit only one line.
  4. In your code after setting the text of 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.

like image 106
AboulEinein Avatar answered Sep 21 '22 19:09

AboulEinein


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

like image 28
Mr Ordinary Avatar answered Sep 20 '22 19:09

Mr Ordinary