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.