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

top aligned labels css
align text in label
label on top of input
html textarea label align: top
css label position
css for label and textbox
label under input
align label left

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?

There is no way to set the vertical alignment of a UILabel by default in iOS. Instead, you will want to use one of the sizeWithFont methods provided by NSString. Which one you use depends on whether you want a multi-line or single-line label. sizeWithFont:forWidth:lineBreakMode: can be used for single-line labels, while sizeWithFont:constrainedToSize:lineBreakMode: can be used for multi-line labels. You can easily load the font parameter with the font property of the label in question. You can look here for some more details on using these functions.

These methods will return a CGSize struct with the minimum size for your label based on the text in the NSString. Once you have the correct size for the label, you can easily place it at the top of your superview and it will appear to be top-aligned.

The Definitive Guide to Form Label Positioning, Options for positioning form labels. For English-based forms, the main options (1) are: Label on top of the field; Label to  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.

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.

ui.label, API Reference Webix Docs, API Reference. This page contains ui.label documentation to help in learning the library. ui.label. A non-editable text control. view of the component. getTopParentView, returns the top parent view align, the alignment of the control within the parent container popup, allows to attach pop-up menu to the button. NSTextAlignment. UILabel if text in label is too short to fit in the number of lines used for that label, the text is displayed in the vertical center of the label. To vertically align the text at the top of the UILabel

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

Sams Teach Yourself iPhone Application Development in 24 Hours, The views you are adding are convenient objects that we can attach gestures Drag a fifth UILabel instance to the main view, and center it at the top of the screen. Use the Attributes Inspector to set it to align center. This will be the label we use to provide feedback to the user. Change the label's default text to Do something! drag UPWARDS from the label to the view, release and choose Top Space to Top Layout Guide; after that go to the label's size inspector and make sure that the constraints all have a constant of Standard. last but not least go to the label's attributes inspector and make sure that the Lines property is set to 0.

I have replaced UILabel with UITextView, because in UITextView, the text sticks to the top.

Just a suggestion, it may be useful for someone.

Label, Attached. A label can attach to a content segment. Attached labels attempt to intelligently pad other content to account for their position, but may not in  Vertically align text to top within a UILabel (20) I have a UILabel with space for two lines of text. Sometimes, when the text is too short, this text is displayed in the vertical center of the label. How do I vertically align the text to always be at the top of the UILabel?

Make a subclass of UILabel

.h file

@property (nonatomic, assign) UIControlContentVerticalAlignment verticalAlignment;

.m file

- (void) drawTextInRect:(CGRect)rect 
{
    if(_verticalAlignment == UIControlContentVerticalAlignmentTop ||  _verticalAlignment == UIControlContentVerticalAlignmentBottom)    
    {
         // If one line, we can just use the lineHeight, faster than querying sizeThatFits
         const CGFloat height = floorf(((self.numberOfLines == 1) ? ceilf(self.font.lineHeight) : [self sizeThatFits:self.frame.size].height));
          rect.origin.y = floorf(((self.frame.size.height - height) / 2.0f) * ((_verticalAlignment == UIControlContentVerticalAlignmentTop) ? -1.0f : 1.0f));
    }
    [super drawTextInRect:rect];
}


- (void) setVerticalAlignment:(UIControlContentVerticalAlignment)newVerticalAlignment
{
     _verticalAlignment = newVerticalAlignment;
     [self setNeedsDisplay];
}

I used this for Vertical Alignment .

How do I align a label and a textarea?, You need to put them both in some container element and then apply the alignment on it. For example: .formfield * { vertical-align: middle; } iOS: How to vertically align text within a UILabel 10:50 AM 1 Share 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.

[Solved] Put dynamic labels around 3D model, I would like to arrange multiple text labels around a 3D model to point out specific points of it. on the top of it and compute the distance from the center of the circle which is nearly impossible to do since my labels are on my screen. nasty code outside of anything you attach to an object, the 3 classes  label.textAlignment = NSTextAlignmentLeft; Any value in the NSTextAlignment enum is valid: NSTextAlignmentLeft, NSTextAlignmentCenter, NSTextAlignmentRight, NSTextAlignmentJustified, NSTextAlignmentNatural. Vertical alignment in UILabel is not supported out of the box: Vertically align text to top within a UILabel

InputLabel API, See CSS API below for more details. color, 'primary' | 'secondary', The color of the component. It supports those theme colors that make sense for this  You can use an NSAttributedString but in order to change the alignment, you need separate paragraphs in the label, then you can set a paragraph style for the paragraph you want to right align. So let's say I create a label in the Storyboard Designer with the text "Left Right" (note there is no way to enter a return character in the storyboard

How to left center align the text of a label. - Base Camp, View This Post. Ravi Shankar dragged and dropped a label.assigned a text.Set the content Still while running the application, text is Center Top aligned. The last constraint will make sure UIView stretches with the content of UILabel it hosts. Constraints for UILabel: Add UILabel as a subView of previously added UIView; Let the parameter numberOfLines remain to 1. This will make sure UILabel won't stretch vertically; Add constraints for leading, trailing, top and bottom edges with 10px padding

Comments
  • Here's an answer I gave to a similar question: stackoverflow.com/questions/1054558/…
  • This link did it for me. Thanks nevan king! Anyone having the same issue as me can go to the link above, or check out my answer below, and I think you will find the solution.
  • QUICK TRICK ... simply sizeToFit
  • You can also call sizeToFit on the label to set the frame to its contents.
  • Also setting the constraint for the height to >= whatever the original height was will allow the label to grow and stay upper left with the text in the container.
  • Ran into this with a UICollectionViewCell. The regular sizeToFit wasn't working. Your autolayout suggestion worked in this instance. Thanks!
  • Thanks for your post, it works like a charm on iOS 11 for those who wonder :)
  • make sure you use <= not just set = to a value less than the maximum height. I was not getting this but now it works
  • what do u mean by step 1 and step 3?
  • Working for me with iOS 11 code updated: UILabel *iconLabel = [UILabel.alloc initWithFrame:CGRectMake(0, 0, size, size)]; [iconLabel setTextColor:[UIColor whiteColor]]; [iconLabel setTextAlignment:NSTextAlignmentCenter]; CGSize labelSize = iconLabel.frame.size; iconLabel.frame = [icon boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil]; [iconLabel setText:icon]; [iconLabel setNumberOfLines:1]; [iconLabel sizeToFit]; return iconLabel;
  • Useful to me :) How do make it multi-line though? I cant seem to find an option which allows this :/
  • Yes it works but plz tell me how should i change its font size. I have my TextView on a custom cell.