Set label height based on text in ios

Source: Internet
Author: User

UILabel * label = [[UILabel alloc] init]; NSString * text = @ "hellohellohellohellohellohellohellohellohellohellohellohellohello"; label. text = text; [label setNumberOfLines: 0]; UIFont * font = [UIFont fontWithName: @ "Arial" size: 14]; // set the font label. font = font; CGSize size = CGSizeMake (300,200 000000f); size = [text sizeWithFont: font constrainedToSize: size lineBreakMode: NSLineBreakByCharWrapping]; [label set Frame: CGRectMake (10, 0, size. width, size. height)]; [self. view addSubview: label]; lineBreakMode: sets the display mode when the label text is too long. Label. lineBreakMode = NSLineBreakByCharWrapping. Label. lineBreakMode = NSLineBreakByClipping; cut the content length that is the same as the text width, and delete the second half. Label. lineBreakMode = NSLineBreakByTruncatingHead; the text above is ...... To display the text at the end. Label. lineBreakMode = NSLineBreakByTruncatingMiddle; the content in the middle is ...... To display the text at the beginning and end. Label. lineBreakMode = NSLineBreakByTruncatingTail ...... To display the text content of the header. Label. lineBreakMode = NSLineBreakByWordWrapping; displayed in the unit of words. The latter part is omitted and not displayed. Http://www.xue5.com/Mobile/iOS/673562.html this is the code on the Internet and can run normally. However, there is still a problem, that is, the label is dynamically generated here. If a dragged label in the view is connected to a corresponding attribute in the controller, the setFrame of this label is always invalid. In this case, you can click "size inspector", find the height equals in constraints, click the gear pattern behind it, and select and edit, set the value of Priority to a smaller value in height constraints.

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.