Overview
This article describes some general uses of label. There is a lot of detail in meeting the UI requirements, so a more flexible use of the label can be done. First of all, thank Bo Master kc for his wonderful blog post.
The main contents of this article are as follows:
1.some basic methods and properties of Labe
2.how the Label control automatically adapts to the size of the text in the control
3. design Other related classes in the label
Some basic methods and properties of Labe
As for the basic methods and properties, see the following example:
UILabel *label = [[UILabel alloc]init]; Label.backgroundcolor = [Uicolor redcolor]; Label.text = @ "Hushunfeng"; Label.font = [Uifont systemfontofsize:12.0f]; Label.textcolor = [Uicolor Yellowcolor];
The main use of the font is to be skilled, and color is usually not simple to call this method to set.
How the Label control automatically adapts to the size of the text in the control
In practical engineering, it is usually necessary to synchronize the size of the label control according to the size of the text, so that the label control can "wrap" the text exactly, without wasting extra space, which also facilitates the layout of the view.
There are two ways to provide this functionality:
- For display invocation of single-line text data - (cgsize) sizewithattributes: (Nsdictionary *) attrs; method to get the text width and height.
- Display invocation for multiline text data -(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: ( Nsdictionary *) Attributes Context: (Nsstringdrawingcontext *) context; method to get the width and height of the text, and note that you need to set the NumberOfLines property of the text control to 0 before this.
Let's start with a single-line example:
UILabel *label = [[UILabel alloc]init]; Label.backgroundcolor = [Uicolor blackcolor]; Label.text = @ "Hushunfeng"; Label.font = [Uifont systemfontofsize:12.0f]; Label.textcolor = [Uicolor greencolor]; cgsize usernamesize=[label.text sizewithattributes:@{nsfontattributename: [Uifont systemFontOfSize:11]}]; Label.frame = CGRectMake (n, A, usernamesize.width, usernamesize.height);
Note the last two lines of code. we get the width and height of the font through text, and then we set the label frame.
On the use of--label in iOS development