A summary of common settings for Uilabel text display in IOS applications _ios

Source: Internet
Author: User
Tags set background truncated

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:

Label.numberoflines = 0;

Dynamic adjustment of Uilabel height
Copy Code code as follows:

[Label SizeToFit];

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];

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.