1//Create label
UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0,0,0,0)];
2//Text
NSString *string = @ "11111111122222222233333333";
Uifont *font = [Uifont fontwithname:@ "Arial" size:12];
3//Set automatic line number and character wrapping (text multiline display)
(1) Label.linebreakmode = Uilinebreakmodewordwrap;
[Label setnumberoflines:0]; Number of rows is unlimited
(2) Label.numberoflines = 1; The number of rows is set to 1, and when not set, the system defaults to 1 rows.
Label.adjustsfontsizetofitwidth =yes; Sizing based on font size (1 only)
4//Set a line height limit
Cgsize size = Cgsizemake (320,2000);
5//Calculate the actual frame size and change the label frame to the actual size
Cgsize labelsize = [string Sizewithfont:font constrainedtosize:size linebreakmode:uilinebreakmodewordwrap];
[Label Setframe:cgrectmake: (0,0, Labelsize.width, Labelsize.height)];
6//Put on view
[Self.view AddSubview:self.label];
Tips:
1 when the label size is used with the SizeToFit method, the value stored in the property is taken into account when sizing: [Label SizeToFit];
2 when the text content is many, label cannot display all when the label replaces the text content with the ellipsis
Label text ellipsis settings please link: http://www.cnblogs.com/luoyubuku/p/3816922.html