After ios7, the frame method is calculated based on the UILabel text. ios7uilabel

Source: Internet
Author: User

After ios7, the frame method is calculated based on the UILabel text. ios7uilabel

Ios7 determines the width and height of the label based on the text and font size of the label.

The official method is:

-(CGRect) boundingRectWithSize :( CGSize) size options :( NSStringDrawingOptions) options attributes :( NSDictionary *) attributes context :( NSStringDrawingContext *) context NS_AVAILABLE_IOS (7_0 );

NSStringDrawingOptions has four enumerated values:

Typedef NS_OPTIONS (NSInteger, NSStringDrawingOptions) {// if the text content exceeds the specified rectangle limit, the text is truncated and a ellipsis is added after the last character. If the NSStringDrawingUsesLineFragmentOrigin option is not specified, NSStringDrawingTruncatesLastVisibleLine = 1 is ignored. <5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. ignored if NSStringDrawingUsesLineFragmentOrigin is not also set. // use line fragement origin instead of baseline origin NSStringDrawingUsesLineFragmentOrigin = 1 when drawing text <0, // The sp Ecified origin is the line fragment origin, not the base line origin // use line spacing when the calculated row is high. (Translator's note: font size + line spacing = line spacing) NSStringDrawingUsesFontLeading = 1 <1, // Uses the font leading for calculating line heights // use the metachiname (instead of the printed font) when calculating the layout ). NSStringDrawingUsesDeviceMetrics = 1 <3, // Uses image glyph bounds instead of typographic bounds} NS_ENUM_AVAILABLE_IOS (6_0 );

 

Attributes is the attribute of text font: this parameter sets the font size.

Context is a context object that contains information about how to adjust the font spacing and scale. Finally, the information contained in the object will be used for text painting. This parameter can be nil.

 

NSDictionary * attributes1 ={ NSFontAttributeName: [UIFont systemFontOfSize: 20], identifier: [UIColor redColor]}; UILabel * titleLabel = [UILabel new]; titleLabel. text = @ "Long live demasea, the day when the sword was rebuilt, we had to solve it in a difficult way when the Knight returned. I could have been a little lighter! "; TitleLabel. numberOfLines = 0; // multi-row display, computing height titleLabel. textColor = [UIColor blackColor]; titleLabel. backgroundColor = [UIColor greenColor]; CGSize titleSize = [titleLabel. text boundingRectWithSize: CGSizeMake (300, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin attributes: attributes1 context: nil]. size; titleLabel. frame = CGRectMake (10, 64, titleSize. width, titleSize. height); [self. view addSubview: titleLabel];

 

As follows:

 

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.