IOS: Set UILabel line spacing and iosuilabel line spacing

Source: Internet
Author: User

IOS: Set UILabel line spacing and iosuilabel line spacing

// Create UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (7, 5, TABLEVIEW_WIDTH-HEADIMGVIEW_HEIGHT-20-24-20, 0)]; // automatically wrap the label. numberOfLines = 0; // set the label content width CGFloat textWidth = 414; // obtain the data NSString * text = model. content; // create an NSMutableAttributedString instance and pass text to NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString: text]; // create an NSMutableParagraphStyle instance NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init]; // set the line spacing [style setLineSpacing: 10.0f]; // determine whether the content length is greater, if not, set the content width to the row width (if the content is smaller than the row width, the Label length is too short. If the Label has a background color, the layout effect will be affected) NSInteger leng = textWidth; if (attStr. length <textWidth) {leng = attStr. length;} // set the attStr style [attStr addAttribute: NSParagraphStyleAttributeName value: style range: NSMakeRange (0, leng)] based on the given length and style; // The Label gets the attStr style label. attributedText = attStr; // Label adaptive size [label sizeToFit]; // set the Label height label. height = label. frame. size. height;
This method can not only set the Label line spacing, but also set the adaptive height.

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.