Creating Uilabel Objects
Copy Code code as follows:
uilabel* label = [[Uilabel alloc] initWithFrame:self.view.bounds];
Set display text
Copy Code code as follows:
Label.text = @ "This is a uilabel Demo,";
Set Text font
Copy Code code as follows:
Label.font = [Uifont fontwithname:@ "Arial" size:35];
Set Text color
Copy Code code as follows:
Label.textcolor = [Uicolor Yellowcolor];
Set Text horizontal display position
Copy Code code as follows:
Label.textalignment = Uitextalignmentcenter;
Set Background color
Copy Code code as follows:
Label.backgroundcolor = [Uicolor Bluecolor];
Set word-wrapping style
Copy Code code as follows:
Label.linebreakmode = Uilinebreakmodewordwrap;
Sets whether a label can display more than 0 rows
Copy Code code as follows:
Dynamic adjustment of Uilabel height
Copy Code code as follows:
Dynamically set the height of the Uilabel according to the content size
Copy Code code as follows:
Cgsize size = [Label.text sizeWithFont:label.font constrainedToSize:self.view.bounds.size Linebreakmode: Label.linebreakmode];
CGRect rect = label.frame;
Rect.size.height = Size.Height;
Label.frame = rect;
Line Wrapping Mode
Copy Code code as follows:
typedef enum {
Uilinebreakmodewordwrap = 0,//with space as the boundary, keep the whole word
Uilinebreakmodecharacterwrap,//Keep whole character
Uilinebreakmodeclip,//until the border
Uilinebreakmodeheadtruncation,//omit start, to ... Replace
Uilinebreakmodetailtruncation,//omit end, to ... Replace
Uilinebreakmodemiddletruncation,//omit middle, to ... Instead, multiple rows are used for the last line
} Uilinebreakmode;
Other:
Uilinebreakmodewordwrap = 0,
Wraps the word as a unit, truncated in units.
Uilinebreakmodecharacterwrap,
Wraps in character units, truncated in characters.
Uilinebreakmodeclip,
Wraps a word as a unit. truncated in characters.
Uilinebreakmodeheadtruncation,
Wraps a word as a unit. If it is a single line, the start section has an ellipsis. If it is more than one line, there is an ellipsis in the middle and a 4 character after the ellipsis.
Uilinebreakmodetailtruncation,
Wraps a word as a unit. Whether it's a single line or multiple lines, there are ellipses at the end.
Uilinebreakmodemiddletruncation,
Wraps a word as a unit. Whether it is a single line or multiple lines, there is an ellipsis in the middle, and only 2 characters after the ellipsis.
Tip: Automatically adapt to width and height based on string length
Copy Code code as follows:
//This frame is initially set up, it doesn't matter, it will reset its size later.
Uilabel *label = [[Uilabel alloc] Initwithframe:cgrectmake (0,0,0,0)];
label.numberoflines = 0;
label.backgroundcolor = [Uicolor Clearcolor];
nsdictionary *attributes = @{nsfontattributename:[uifont SYSTEMFONTOFSIZE:20],};
nsstring *str = @ "ABCDEFG you get the class";
cgsize textsize = [str boundingrectwithsize:cgsizemake (MB) options: Nsstringdrawingtruncateslastvisibleline attributes:attributes context:nil].size;;
[Label Setframe:cgrectmake (MB, textsize.width, Textsize.height)] ;
label.textcolor = [Uicolor Greencolor];
label.text = str;
[Self.view Addsubview:label];