Tips for using Uilabel to set fonts in IOS development _ios

Source: Internet
Author: User

First, initialization

Copy Code code as follows:

Uilabel *mylabel = [[Uilabel alloc] Initwithframe:cgrectmake (40, 40, 120, 44)];

[Self.view Addsubview:mylabel];

Second, set the text

1, set the default text

Copy Code code as follows:

NSString *text = @ "Label text";
MyLabel.Text = text;

Effect:

2, set the label text (this property is iOS6.0 before appearing, if not necessary, do not recommend the use of this property)

Copy Code code as follows:

NSString *text = @ "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:

The effect of keyword red

Copy Code code as follows:

NSString *keyword = @ "open source";
NSString *result = @ "Open source China community";

Set Label text
nsmutableattributedstring *attritutestring = [[Nsmutableattributedstring alloc] initwithstring:result];

Get the location and length of the red flag
Nsrange range = [result Rangeofstring:keyword];

Set properties for label text
[Attritutestring setattributes:@{nsforegroundcolorattributename: [Uicolor Redcolor], NSFontAttributeName: [UIFont SYSTEMFONTOFSIZE:17]} Range:range];

Show on label
Label.attributedtext = attritutestring;

3, set the font, if you are using the text in ②, then set the AttributeString properties have been set font and TextColor, directly using the ① when setting text, set the font method
Copy Code code as follows:

Mylabel.font = [Uifont systemfontofsize:13];

4, set the color
Copy Code code as follows:

Mylabel.textcolor = [Uicolor Bluecolor];

5. Set Alignment method
Copy Code code as follows:

mylabel.textalignment = nstextalignmentcenter;//Center

Nstextalignmentleft//left-aligned
Nstextalignmentcenter//Center
Nstextalignmentright//Right alignment
nstextalignmentjustified//the last line of natural alignment
Nstextalignmentnatural//default alignment script


Nstextalignmentjustified and nstextalignmentnatural use of the time will be an error, the program crashes, temporarily do not know when to use, want to know the advice, grateful.
5, Text clipping mode
Copy Code code as follows:

Nslinebreakbywordwrapping = 0,//with a space boundary, preserving the word
Nslinebreakbycharwrapping,//Keep whole character
Nslinebreakbyclipping,//simple tailoring, to the border
Nslinebreakbytruncatinghead,//In accordance with the "... Text "Show
Nslinebreakbytruncatingtail,//According to the words ... Text "Show
Nslinebreakbytruncatingmiddle//In accordance with "text ..." Display

Mylabel.linebreakmode = Nslinebreakbytruncatinghead;


7. Set the label enabled property
If set to No, the text color is dimmed to indicate that it is not available and the default is yes.
Copy Code code as follows:

mylabel.enabled = NO;

third, match the text on the label
1. Whether to change font size based on text width
Copy Code code as follows:

Mylabel.adjustsfontsizetofitwidth = YES;
Suppose the text content is @ "Once in the moonlight looking at fireworks, had a total of watching the sunset gradually lowered", the label length is 200, the line does not appear, if you set this property to Yes, it will reduce the font size to display the entire content.

Before and after comparison:

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

Copy Code code as follows:

When this property is yes, the label may change the letter spacing of the label text so that the text fits more within the bounds of the label. The string of this property, regardless of the cropping pattern of the row of the current row. The default value for this property is no.
Mylabel.adjustsletterspacingtofitwidth = NO;

Personal use, did not find any difference, do not know exactly when to play a role.

3, set to baseline line
Copy Code code as follows:

Mylabel.adjustsfontsizetofitwidth = yes;//Adjust Baseline Position This property must be set to YES

Mylabel.baselineadjustment = Uibaselineadjustmentalignbaselines;


This property has three values to choose from
Copy Code code as follows:

Uibaselineadjustmentalignbaselines//text is aligned to the top of the label, and the default value
Uibaselineadjustmentaligncenters//Text centerline aligns with label centerline
Uibaselineadjustmentnone//text is aligned to the bottom of the label

4, the minimum font size, when the font is less than the minimum value is invalid, display this property value
Before iOS6.0: minimumfontsize

After iOS6.0: Minimumscalefactor

Copy Code code as follows:

Mylabel.minimumscalefactor = 10.0;//Default value is 0, the current font size

5, Number of lines
Copy Code code as follows:

Mylabel.numberoflines = 2;//label number of rows

6, highlighting
Copy Code code as follows:

mylabel.highlighted = yes;//is highlighted
Mylabel.highlightedtextcolor = [Uicolor redcolor];//highlight color; This property when you set the Titlelabel of a button, whether the highlighted is yes or no, The caption displays this highlight color when the button is pressed

7, Shadow
Copy Code code as follows:

Mylabel.shadowcolor = [Uicolor graycolor];//shadow color, default to nil
Mylabel.shadowoffset = Cgsizemake (1, 1);//The offset point of the shadow

Four, label location
1, calculate Uilabel with the font after the height of multiple lines
Copy Code code as follows:

CGRect result,bounds;
bounds = CGRectMake (0, 0,200, 300);
Heightlabel = [MyLabel textrectforbounds:bounds limitedtonumberoflines:20];//calculates the frame of the label after 20 lines
NSLog (@ "%f", heightLabel.size.height);

2. Draw text to specified area
Copy Code code as follows:

-(void) Drawtextinrect: (cgrect) rect
This method needs to be overloaded and then called by subclasses, and when overridden, call super can be drawn by default graphic properties, and if you completely rewrite the drawing function, you don't have to call super.

PS: Questions about the font name
Use custom fonts under iOS compare egg pain is the name do not know what, most fonts can be opened in the font book can be seen, such as normal xxx-regular, but sometimes the name is not this, there may be xxx just, It could also be xxxitalic (not xxx-italic). The name is wrong, the font set up is definitely empty.

What about it?

There is a way, there is a piece of code can be all the current font name output.

Such as:

1. Add the xxx font to resouce;

2.info.plist fonts provided by application Add font file name

3. Run the following code

Copy Code code as follows:

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, such as the following, font: The following is 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. Nature is used, as

Copy Code code as follows:

Uifont *font_regular = [Uifont fontwithname:@ "COURIERNEWPS-ITALICMT" size:84];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.