IOS _ dynamically determines the Label width and height based on the word count

Source: Internet
Author: User

IOS _ dynamically determines the Label width and height based on the word count
The following method is used in iOS7:

-(CGSize) sizeWithAttributes :( NSDictionary *) attrs;


Replace-(CGSize) sizeWithFont :( UIFont *) font method in outdated iOS6

// IOS7_API _ dynamically determine the Label width and height based on the number of words. // set the Label font HelveticaNeue Courier UIFont * fnt = [UIFont fontWithName: @ "HelveticaNeue" size: 24366f. font = fnt; // obtain the size of NSString CGSize = [_ nameLabel. text sizeWithAttributes: [NSDictionary dictionaryWithObjectsAndKeys: fnt, NSFontAttributeName, nil]; // name of H CGFloat nameH = size. height; // W CGFloat nameW = size. width; _ nameLabel. frame = CGRectMake (0, 0, nameW, nameH );


In iOS7, use boundingRectWithSize: options: attributes: context: to replace sizeWithFont: constrainedToSize: lineBreakMode: Method
// 4. dynamically determine the frame // Width W CGFloat contentW = self. bounds. size. width-_ content. frame. origin. x-kMargin; // The label font HelveticaNeue Courier UIFont * fnt = [UIFont fontWithName: @ "HelveticaNeue" size: 18366f]; _ content. font = fnt; _ content. numberOfLines = 0; _ content. lineBreakMode = NSLineBreakByWordWrapping; // use the following method in iOS7 to replace sizeWithFont: constrainedToSize: lineBreakMode: Method CGRect tmpRect = [_ content. text boundingRectWithSize: CGSizeMake (contentW, 1000) options: Optional attributes: [NSDictionary attributes: fnt, NSFontAttributeName, nil] context: nil]; // height H CGFloat contentH = tmpRect. size. height; NSLog (@ "adjusted display width: % f, display height: % f" contentW, contentH); _ content. frame = CGRectMake (0, 0, contentW, contentH );


Appendix: API reference


BoundingRectWithSize: options: attributes: context:


Calculates and returns the bounding rect for the specified er drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.

-(CGRect) boundingRectWithSize :( CGSize) size options :( NSStringDrawingOptions) options attributes :( NSDictionary *) attributes context :( NSStringDrawingContext *) context

Parameters

Size

The size of the rectangle to draw in.

Options

String drawing options.

Attributes

A dictionary of text attributes to be applied to the string. these are the same attributes that can be applied to anNSAttributedString object, but in the case ofNSString objects, the attributes apply to the entire string, rather than ranges within the string.

Context

The string drawing context to use for the specified er, specifying minimum scale factor and tracking adjustments.

Return Value

The bounding rect for the specified er drawn using the given options and display characteristics. The rect origin returned from this method is the first glyph origin.

Discussion

To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.

This method returns fractional sizes (in the size component of the returnedCGRect); to use a returned size to size views, you must raise its value to the nearest higher integer using theceil function.

This method returns the actual bounds of the glyphs in the string. some of the glyphs (spaces, for example) are allowed to overlap the layout constraints specified by the size passed in, so in some cases the width value of the size component of the returned CGRect can exceed the width value of the size parameter.

Availability

  • Available in iOS 7.0 and later.

    See Also

    • -DrawInRect: withAttributes:

      Declared In

      NSStringDrawing. h



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.