Align the top of the Uilabel text

Source: Internet
Author: User

Original article link: http://blog.devtang.com/2011/11/20/set-uilabel-text-align-top/

The default Uilabel is aligned vertically, and if your uilabel height has multiple lines, it will automatically center vertically when the content is low.

As shown (image from StackOverflow):

It is depressing 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 way to do it yourself. Several methods are available on the stackoverflow.com to achieve top-aligned effects.

Method One

When displaying text, first calculate how wide and how high the current text is displayed, and then change the size of the corresponding Uilabel to the corresponding width and height. The phase of this method is as follows:

When displaying text, first calculate how wide and how high the current text is displayed, and then change the size of the corresponding Uilabel to the corresponding width and height. The phase of this method is as follows:

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:selfdatelabel. Linebreakmode];
CGRect dateframe = CGRectMake (Ten, ten, datestringsize. height);
Self. Datelabel. frame = Dateframe;
Method Two

This method is more simple and rude, but effective. The method is to add more \ n after the text.
It is important to note that after \ n is added at least one space, otherwise the extra \ n will be ignored by Uilabel. From this point of view, Uilabel seems to be too "smart".

The method is as follows:

The code for this method is as follows:

for (int i=0; i<newlinestopad; i++)
Self. Text = [self. Text stringbyappendingstring:@ "\ n"];
Method Three

The most orthodox method, using the OBJECTIVE-C category feature, modifies the Uilabel drawing code. The sample code is as follows:

--File:uilabel+verticalalign.h
#pragma Mark VerticalAlign
@interfaceUILabel (VerticalAlign)
- (void) AlignTop;
- (void) AlignBottom;
@end

--FILE:UILABEL+VERTICALALIGN.M
@implementationUILabel (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;
Forint 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 the simple method of violence two, what about you?

Resources

Http://stackoverflow.com/questions/1054558/how-do-i-vertically-align-text-within-a-uilabel

https://discussions.apple.com/thread/1759957?threadID=1759957

Align the top of the Uilabel text

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.