IOS UILabel obtains the height when customizing the row spacing. iosuilabel

Source: Internet
Author: User

IOS UILabel obtains the height when customizing the row spacing. iosuilabel

This article describes how to obtain the UILabel height for custom row spacing. You need to use the open-source UILabel control: TTTAttributedLabel.

Attached https://github.com/TTTAttributedLabel/TTTAttributedLabel

After the download, add it to the project and import the header file

 #import "TTTAttributedLabel.h"

Directly Add code

NSString * str = @ "UILabel obtains the height when customizing the row spacing, and UILabel obtains the height when customizing the row spacing, the height is obtained when UILabel customizes the row spacing, when UILabel customizes the row spacing, and when UILabel customizes the row spacing. "; // create tttLabel TTTAttributedLabel * tttLabel = [[TTTAttributedLabel alloc] initWithFrame: CGRectMake (30, 55,300,100)]; tttLabel. lineBreakMode = NSLineBreakByCharWrapping; tttLabel. lineSpacing = 6; // set the row spacing tttLabel. font = [UIFont systemFontOfSize: 12]; tttLabel. numberOfLines = 0; // set the number of rows to 0 [tttLabel setText: str]; tttLabel. textAlignment = NSTextAlignmentLeft; tttLabel. backgroundColor = [UIColor redColor]; [self. view addSubview: tttLabel]; // get the height of tttLabel // first set the same attributes as tttLabel through NSMutableAttributedString, such as row spacing and font NSMutableAttributedString * attrString = [NSMutableAttributedString alloc] initWithString: str]; // customize the line spacing between str and TTTAttributedLabel * paragrapStyle = [[NSMutableParagraphStyle alloc] init]; [paragrapStyle setLineSpacing: 6]; // set the line spacing [attrString addAttribute: NSParagraphStyleAttributeName value: paragrapStyle range: NSMakeRange (0, str. length)]; // set the font [attrString addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 12] range: NSMakeRange (0, str. length)]; // obtain the height of UILabel for custom row spacing CGFloat height = [TTTAttributedLabel failed: attrString withConstraints: CGSizeMake (300, MAXFLOAT) limitedToNumberOfLines: 0]. height; // re-change the frame height of tttLabel CGRect rect = tttLabel. frame; rect. size. height = height; tttLabel. frame = rect;

 

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.