Uilabel text vertical top Alignment Method

Source: Internet
Author: User

I also don't know Why uilabel itself does not provide the vertical top Alignment Method for the text, which is a bit dizzy. Let's create a simple uilabel to see:

[Box Type = "info"]

Uilabel * mylabel = [[uilabel alloc] initwithframe: cgrectmake (10, 10,300,100)];

[Mylabel settext: @ "Apple IOs (iPhone Operation System) is a handheld device operating system developed by Apple. Apple first announced the system at the Macworld conference in January 9, 2007, initially designed for the iPhone "];

[Mylabel setfont: [uifont fontwithname: @ "Arial" Size: 14.0];

[Mylabel setbackgroundcolor: [uicolor bluecolor];

[Mylabel settextcolor: [uicolor whitecolor];

[Self. View addsubview: mylabel];

[/Box]

The effects shown on the IOS simulator are as follows:

 

This is obviously not what we want. Let's add another line of code:

Mylabel. numberoflines = 0;

The result is:

 

The display is a bit normal, but how to align the top of the text?

Implementation Code:

[Box Type = "info"]

Uilabel * mylabel = [[uilabel alloc] init]; // withframe: cgrectmake (10, 10,300,100)];

 

Uifont * strfont = [uifont fontwithname: @ "Arial" Size: 14.0];

 

[Mylabel setfont: strfont];

[Mylabel setbackgroundcolor: [uicolor bluecolor];

[Mylabel settextcolor: [uicolor whitecolor];

Mylabel. numberoflines = 0;

 

Cgsize maximumsize = cgsizemake (300,999 );

Nsstring * string = @ "Apple IOs (iPhone Operation System) is a handheld device operating system developed by Apple. Apple first announced the system at the Macworld conference in January 9, 2007, initially designed for iPhone use ";

Cgsize stringsize = [String sizewithfont: strfont

Constrainedtosize: maximumsize

Linebreakmode: mylabel. linebreakmode];

[Mylabel settext: String];

Cgrect strframe = cgrectmake (10, 10,300, stringsize. Height );

Mylabel. Frame = strframe;

[Self. View addsubview: mylabel];

 

[/Box]

The running result is as follows:

 

If you have a better way to align the vertical top of uilabel, you must share it.

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.