Align the top of uilabel text

Source: Internet
Author: User

In xcode, the default uilabel is vertically centered and aligned. If your uilabel has multiple rows in height, the uilabel is automatically centered vertically when the content is small.

As shown in (picture from stackoverflow ):

What is depressing is that uilabel does not provide the option to set its vertical alignment. So if you want to align the top of your text, you need to find a solution. Stackoverflow.com provides several methods to achieve top alignment.

Method 1

When displaying text, first calculate how wide and high the current text needs to be displayed, and then change the size of the corresponding uilabel to the corresponding width and height. This method is similar to the following:

When displaying text, first calculate how wide and high the current text needs to be displayed, and then change the size of the corresponding uilabel to the corresponding width and height. This method is similar to the following:

(In most cases, you can set the maximum frame and maximum numberofline of the label. [label sizetofit] can automatically adjust the Label Size Based on the Content conditions.

12345678
CGSize maximumSize = CGSizeMake(300, 9999);NSString *dateString = @"The date today is January 1st, 1999";UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];CGSize dateStringSize = [dateString sizeWithFont:dateFont    constrainedToSize:maximumSize    lineBreakMode:self.dateLabel.lineBreakMode];CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);self.dateLabel.frame = dateFrame;
Method 2

This method is simpler and more crude, but effective. The method is to add more \ n after the text. Note that at least one space must be added after \ n; otherwise, the redundant \ n will be ignored by uilabel. From this point of view, uilabel seems too "smart.

The method is as follows:

The code for this method is as follows:

12
for(int i=0; i<newLinesToPad; i++)    self.text = [self.text stringByAppendingString:@"\n "];
Method 3

The most orthodox method, using the category feature of objective-C, modifies the uilabel rendering code. The sample code is as follows:

1234567891011121314151617181920212223242526272829
// -- file: UILabel+VerticalAlign.h#pragma mark VerticalAlign@interface UILabel (VerticalAlign)- (void)alignTop;- (void)alignBottom;@end// -- file: UILabel+VerticalAlign.m@implementation UILabel (VerticalAlign)- (void)alignTop {    CGSize fontSize = [self.text sizeWithFont:self.font];    double finalHeight = fontSize.height * self.numberOfLines;    double finalWidth = self.frame.size.width;    //expected width of label    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;    for(int i=0; i<newLinesToPad; i++)        self.text = [self.text stringByAppendingString:@"\n "];}- (void)alignBottom {    CGSize fontSize = [self.text sizeWithFont:self.font];    double finalHeight = fontSize.height * self.numberOfLines;    double finalWidth = self.frame.size.width;    //expected width of label    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;    for(int i=0; i<newLinesToPad; i++)        self.text = [NSString stringWithFormat:@" \n%@",self.text];}@end

I chose simple and violent method 2. What about you?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.