Align the top of the Uilabel text

Source: Internet
Author: User

http://blog.devtang.com/blog/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:

12345678      
CgsizeMaximumSize=Cgsizemake(300,9999);NSString*DateString=@ "The date today is January 1st, 1999";Uifont*Datefont=[UifontFontwithname:@ "Helvetica"Size14];CgsizeDatestringsize=[DateStringSizewithfont:Datefont constrainedtosize:maximumsize  Linebreakmode:self. Datelabel. Linebreakmode; cgrect dateframe = CGRectMake< Span class= "P" > (1010300< Span class= "P", 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:

12   
 for (int i=0i<newlinestopadi++)  self.< span class= "n" >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:

12345< Span class= "Line-number" >6789< Span class= "Line-number" >10111213 141516 17181920 212223 24252627 2829           
--File:uilabel+verticalalign.h#pragma Mark VerticalAlign@interfaceUILabel(verticalalign)-(void)AlignTop;-(void)AlignBottom;@end--FILE:UILABEL+VERTICALALIGN.M@implementationUILabel(verticalalign)-(void)AlignTop{ CgsizeFontSize=[Self.TextSizewithfont:Self.Font]; DoubleFinalheight=FontSize.Height*Self.NumberOfLines; DoubleFinalwidth=Self.Frame.Size.Width;Expected width of label CgsizeThestringsize=[Self.TextSizewithfont:Self.FontConstrainedtosize:Cgsizemake(Finalwidth,Finalheight)Linebreakmode:Self.Linebreakmode]; IntNewlinestopad=(Finalheight-Thestringsize.Height)/FontSize.Height; For(IntI=0;I<Newlinestopad;I++) Self.Text=[Self.TextStringbyappendingstring:@"\ n"];}-(void)AlignBottom{ CgsizeFontSize=[Self.TextSizewithfont:Self.Font]; DoubleFinalheight=FontSize.Height*Self.NumberOfLines; DoubleFinalwidth=Self.Frame.Size.Width;Expected width of label CgsizeThestringsize=[Self.TextSizewithfont:Self.FontConstrainedtosize:Cgsizemake(Finalwidth,Finalheight)Linebreakmode:Self.Linebreakmode]; IntNewlinestopad=(Finalheight-Thestringsize.Height)/FontSize.height; for (int i=0< Span class= "P"; i<newlinestopadi++)  self.< span class= "n" >text = [nsstring stringwithformat: @ "\n%@" ,selftext} @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.