1, according to the text content in the label word wrap display method
1>, recognize two properties
@property(nonatomic) nsinteger numberoflines;
@property(nonatomic) nslinebreakmode linebreakmode;
Where Nslinebreakmode is an enumeration type
typedef Ns_enum(Nsinteger, Nslinebreakmode) {/* What to does with long lines */
nslinebreakbywordwrapping = 0,/ * Wrap at word boundaries, default * /
nslinebreakbycharwrapping,/ * Wrap at character boundaries * /
nslinebreakbyclipping,/ * Simply clip * /
Nslinebreakbytruncatinghead, /* Truncate at Head of line: "... wxyz" * *
Nslinebreakbytruncatingtail, /* Truncate at Tail of line: "ABCD ..." */
Nslinebreakbytruncatingmiddle /* Truncate middle of line: "AB ... YZ "* *
} Ns_enum_available_ios(6_0);
2> method
The first step: Set the number of rows that the label can display, that is, set the NumberOfLines property, Label.numberoflines = 0; When the property is 0, there is no limit
The second step: set the display of the label content, and set the Linebreakmode property Label.linebreakmode = nslinebreakbywordwrapping; indicate adaptive
Step three: Reset the label frame this thing needs to calculate the frame
3> example (fixed width, adaptive calculation height)
1 Initialize the label and set the property
UILabel *label = [[UILabel alloc] initwithframe:cgrectmake(0, 0, 300, )];
Label. NumberOfLines = 0;
Label. Linebreakmode = nslinebreakbywordwrapping;
//2 Initialize The content displayed in the label
nsstring *text = @ " purple mo Mortals, suddenly look back." How much of the Chunhuaqiuyue ; How much of the dead water agarwood ; How many vows, such as the scenery along the way flowers Blossom Xie. The love of the world tenderness how many speechless feelings, sentiment deep edge shallow wind blowing scattered more conspicuous poly discrete. Flowers are sentient beings, and flowers fall unintentionally. The comer is swiftness, the person who goes is the lake and river forget. I see you in the crowd when it comes to the origin. You disappear into the crowd when the rim is extinguished. ";
//3 The width of the fixed label is unchanged , the height of the label is calculated
cgsize TextSize = [text boundingrectwithsize:cgsizemake (maxfloat) options: Nsstringdrawinguseslinefragmentorigin attributes:@{ nsfontattributename:[uifont systemfontofsize:14]} context:nil]. Size
cgrect rect = label. Frame;
Rect.size. Height = textSize. Height;
Label. frame = rect;
How to Uilabel Adaptive size display in iOS development