How to Uilabel Adaptive size display in iOS development

Source: Internet
Author: User

1, according to the text content in the label word wrap display method

1>, recognize two properties

    @property(nonatomic) nsinteger numberoflines;

    @property(nonatomic) nslinebreakmode linebreakmode;

Where Nslinebreakmode is an enumeration type

typedef Ns_enum(Nsinteger, Nslinebreakmode) {/* What to does with long lines */

nslinebreakbywordwrapping = 0,/ * Wrap at word boundaries, default * /

nslinebreakbycharwrapping,/ * Wrap at character boundaries * /

nslinebreakbyclipping,/ * Simply clip * /

Nslinebreakbytruncatinghead, /* Truncate at Head of line: "... wxyz" * *

Nslinebreakbytruncatingtail, /* Truncate at Tail of line: "ABCD ..." */

Nslinebreakbytruncatingmiddle /* Truncate middle of line: "AB ... YZ "* *

} Ns_enum_available_ios(6_0);

2> method

The first step: Set the number of rows that the label can display, that is, set the NumberOfLines property, Label.numberoflines = 0; When the property is 0, there is no limit

The second step: set the display of the label content, and set the Linebreakmode property Label.linebreakmode = nslinebreakbywordwrapping; indicate adaptive

Step three: Reset the label frame this thing needs to calculate the frame

3> example (fixed width, adaptive calculation height)

   

1 Initialize the label and set the property

UILabel *label = [[UILabel alloc] initwithframe:cgrectmake(0, 0, 300, )];

Label. NumberOfLines = 0;

Label. Linebreakmode = nslinebreakbywordwrapping;

//2 Initialize The content displayed in the label

nsstring *text = @ " purple mo Mortals, suddenly look back." How much of the Chunhuaqiuyue ; How much of the dead water agarwood ; How many vows, such as the scenery along the way flowers Blossom Xie. The love of the world tenderness how many speechless feelings, sentiment deep edge shallow wind blowing scattered more conspicuous poly discrete. Flowers are sentient beings, and flowers fall unintentionally. The comer is swiftness, the person who goes is the lake and river forget. I see you in the crowd when it comes to the origin. You disappear into the crowd when the rim is extinguished. ";

//3 The width of the fixed label is unchanged , the height of the label is calculated

    cgsize TextSize = [text boundingrectwithsize:cgsizemake (maxfloat) options: Nsstringdrawinguseslinefragmentorigin attributes:@{ nsfontattributename:[uifont systemfontofsize:14]} context:nil]. Size

cgrect rect = label. Frame;

Rect.size. Height = textSize. Height;

Label. frame = rect;

How to Uilabel Adaptive size display in iOS development

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.