How to set the dynamic height of UILabel in IOS development

Source: Internet
Author: User

This is a requirement in the project. to display an overview of a book, two rows are displayed by default. Click the expand button to show all, and click reclaim. Two rows are displayed.

 


At the beginning, a code like this may be written in the button event:


If (isExpand ){

[LblBrief setNumberOfLines: 2];

LblBrief. text = [StringUtil subString: strBrief AndSubLength: 90];

[BtnBrief setImage: [UIImage imageNamed: @ "detailBriefDown"] forState: UIControlStateNormal];

[BtnBrief setImage: [UIImage imageNamed: @ "detailBriefDown"] forState: UIControlStateHighlighted];

} Else {

[LblBrief setNumberOfLines: 0];

LblBrief. text = strBrief;

[BtnBrief setImage: [UIImage imageNamed: @ "detailBriefUp"] forState: UIControlStateNormal];

[BtnBrief setImage: [UIImage imageNamed: @ "detailBriefUp"] forState: UIControlStateHighlighted];

}

You can use setNumberOfLines: 0 to display the summary content. However, if you do not calculate the total height of all content to be displayed, the width and height of the label will not automatically increase.

 

 


Else {

[LblBrief setNumberOfLines: 0];

LblBrief. text = strBrief;


CGSize constraint = CGSizeMake (self. view. frame. size. width-(15*2), 20000.0f );


CGSize size = [strBrief sizeWithFont: [UIFont systemFontOfSize: 10] constrainedToSize: constraint lineBreakMode: UILineBreakModeWordWrap];


[Brief setFrame: CGRectMake (15,160, size. width, size. height)];

 

 

[BtnBrief setImage: [UIImage imageNamed: @ "detailBriefUp"] forState: UIControlStateNormal];

[BtnBrief setImage: [UIImage imageNamed: @ "detailBriefUp"] forState: UIControlStateHighlighted];

 


}


 

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.