Tabelviewcell adaptive height

Source: Internet
Author: User
1. First, set some attributes of the container (which is assumed to be a label) that displays text in the cell, as shown below:

[_ Labelsetnumberoflines: 0]; // set the number of lines of text in the label. 0 indicates adaptive

[_ Labelsetlinebreakmode: nslinebreakbywordwrapping]; // Line Disconnection Mode

[_ Labelsetfont: [uifontsystemfontofsize: 16.0]; // font size


2. In the cell's-(void) layoutsubviews method, reset the label height in the cell (calculate the height based on the adaptive line feed)


Nsstring * text = [_ array objectatindex: indexpath. Row];

Cell. Label. Text = text; // obtain the corresponding data from the data source

Nsdictionary * DIC = [nsdictionarydictionarywithobject: [uifontsystemfontofsize: 16.0] forkey: nsfontattributename]; // The font size here must be the same as the label font size you set.

Cgsize textsize = [textsizewithattributes: DIC]; // calculate the size based on the data in the dictionary.

Cgrect rect = _ label. frame;

Rect. Size. Height = textsize. height;

_ Label. Frame = rect;

3. Finally, modify the cell Row Height and calculate it again in the Protocol method for setting the cell Row Height.


Nsstring * text = [_ array objectatindex: indexpath. Row];

Cell. Label. Text = text; // obtain the corresponding data from the data source

Nsdictionary * DIC = [nsdictionarydictionarywithobject: [uifontsystemfontofsize: 16.0] forkey: nsfontattributename]; // The font size here must be the same as the label font size you set.

Cgsize textsize = [textsizewithattributes: DIC]; // calculate the size based on the data in the dictionary.

Return textsize. height;


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.