Home>

First, initialization

uilabel * mylabel=[[uilabel alloc] initwithframe:cgrectmake (40, 40, 120, 44)];
[self.view addsubview:mylabel];

Setting text

1.Set the default text

nsstring * text [email protected]"label text";
mylabel.text=text;

effect:

2, set the label text (this property appears after ios6.0,If not necessary,(This property is not recommended)

nsstring * text [email protected]"Actually nothing";
nsmutableattributedstring * attributestring=[[nsmutableattributedstring alloc] initwithstring:text];
[attributestring setattributes:@{nsforegroundcolorattributename:[uicolor redcolor], nsfontattributename:[uifont systemfontofsize:17]} range:nsmakerange (2, 1)];
mylabel.attributedtext=attributestring;

effect:

Keyword red effect

nsstring * keyword [email protected]"open source";
nsstring * result [email protected]"open source chinese community";
//Set the label text
nsmutableattributedstring * attritutestring=[[nsmutableattributedstring alloc] initwithstring:result];
//Get the position and length of the red mark
nsrange range=[result rangeofstring:keyword];
//Set the properties of the label text
[attritutestring setattributes:@{nsforegroundcolorattributename:[uicolor redcolor], nsfontattributename:[uifont systemfontofsize:17]} range:range];
//display on label
label.attributedtext=attritutestring;

3. Set the font,If you use the text in ②,The font and textcolor are already set when setting the attributes of the attributestring. When using ① to set the text,Set font method

mylabel.font=[uifont systemfontofsize:13];

4.Set the color

mylabel.textcolor=[uicolor bluecolor];

5.Set the alignment

mylabel.textalignment=nstextalignmentcenter;//centered
</p>
<p>
nstextalignmentleft //left alignment
nstextalignmentcenter //center
nstextalignmentright //right alignment
nstextalignmentjustified ///The last line is naturally aligned
nstextalignmentnatural //default alignment script

nstextalignmentjustified and nstextalignmentnatural will report errors when used,program crash,I do n’t know when it will be available.Hope to know the advice,Grateful.

5, text cutting method

nslinebreakbywordwrapping=0, //space-bounded,Reserved words
nslinebreakbycharwrapping, //retain the entire character
nslinebreakbyclipping, //simple cropping,To the border
nslinebreakbytruncatinghead, //display according to "... text"
nslinebreakbytruncatingtail, //display as "text ... text"
nslinebreakbytruncatingmiddle //display according to "text ..."
</p>
<p>
mylabel.linebreakmode=nslinebreakbytruncatinghead;

7.Set the label enabled property

If set to no, the text color will be darkened,Indicates that it is unavailable,The default is yes.

mylabel.enabled=no;

Third, match the text on the label1. Whether to change the font size according to the text width

mylabel.adjustsfontsizetofitwidth=yes;
//Suppose the text [email protected]"Have watched fireworks under the moonlight,I have watched the sunset gradually fall ", the length of the label is 200, the line cannot be displayed,Setting this property to yes will reduce the font size,To show everything.

Before and after comparison:

2.Change the spacing between letters to fit the size of the label

//When this attribute is yes, the label may change the letter spacing of the label text,To make the text fit more within the bounds of the label.
A string of this attribute,Regardless of the crop mode of the current line.
The default value of this attribute is no.
mylabel.adjustsletterspacingtofitwidth=no;
//Personally used it a bit,No difference was found,I don't know when it will work.

3.Set the alignment baseline

mylabel.adjustsfontsizetofitwidth=yes;//Adjust the baseline position to set this property to yes
</p>
<p>
mylabel.baselineadjustment=uibaselineadjustmentalignbaselines;

This property has three values ​​to choose from

uibaselineadjustmentalignbaselines //The top of the text is aligned with the centerline of the label,Defaults
uibaselineadjustmentaligncenters //The text centerline is aligned with the label centerline
uibaselineadjustmentnone //The bottom of the text is aligned with the center line of the label

4, the minimum font size,Invalid when the font is smaller than this minimum,Show this attribute value

Before ios6.0:minimumfontsize

After ios6.0:minimumscalefactor

mylabel.minimumscalefactor=10.0;//The default value is 0, which is the current font size

5.Number of rows

mylabel.numberoflines=2;//label line number

6.Highlight

mylabel.highlighted=yes;//whether highlighted
mylabel.highlightedtextcolor=[uicolor redcolor];//Highlighted color;
When setting the titlelabel of this button, whether the highlighted is yes or no, the title will display this highlight color when the button is pressed

7.Shadow

mylabel.shadowcolor=[uicolor graycolor];//Shadow color,Default is nil
mylabel.shadowoffset=cgsizemake (1, 1);//Offset point of shadow

Fourth, the label position1. Calculate the height of the uilabel after multiple lines with the font

cgrect result, bounds;
bounds=cgrectmake (0, 0,200, 300);
heightlabel=[mylabel textrectforbounds:bounds limitedtonumberoflines:20];//Calculate the frame of the label after 20 lines
nslog (@ "%f", heightlabel.size.height);

2.Draw text to the specified area

-(void) drawtextinrect:(cgrect) rect
//Need to overload this method,Then called by the subclass,Calling super when rewriting can draw according to the default graphics properties,If you completely rewrite the drawing function yourself,No need to call super

ps:Questions about font namesIt's more painful to use custom fonts under ios. The name is unknown,Most fonts can be seen in the font book,Like normal xxx-regular, but sometimes the name is not this,It may be xxx, or it may be xxxitalic (not xxx-italic). Name is wrong,The created font is definitely empty.

then what should we do?

There is a way,A piece of code can output all the current font names.

Such as:

1. Add xxx font to resouce;

2.Include font file name in fonts provided by application of info.plist

3. Run the following code

nsarray * familynames=[uifont familynames];
 for (nsstring * familyname in familynames) {
 printf ("family:%s \ n", [familyname utf8string]);
 nsarray * fontnames=[uifont fontnamesforfamilyname:familyname];
 for (nsstring * fontname in fontnames) {
 printf ("\ tfont:%s \ n", [fontname utf8string]);
 }
 }

4. Find your font xxx, as follows, font:followed by the name of the font we want

family:courier new
  font:couriernewpsmt
  font:couriernewps-boldmt
  font:couriernewps-italicmt
  font:couriernewps-bolditalicmt
family:oriya sangam mn
  font:oriyasangammn
  font:oriyasangammn-bold

5. Of course it is used,Such as

uifont * font_regular=[uifont fontwithname:@"couriernewps-italicmt" size:84];
ios
  • Previous Python logging log rotation file does not delete the solution
  • Next Oracle database using sqlplus connection error and direction key garbled solution