Ios_ dynamically determine label width based on word count

Source: Internet
Author: User

IOS7 uses the following methods

-(cgsize) sizewithattributes: (nsdictionary *) attrs;


in lieu of outdated iOS6. -(cgsize) Sizewithfont: ( Uifont *) Font method

    Ios7_api_ dynamically determines the label width        //Setting label font helveticaneue  Courier    uifont *fnt = [Uifont fontwithname:@] based on word count Helveticaneue "size:24.0f";    _namelabel.font = fnt;    Get nsstring size according to font    cgsize size = [_namelabel.text sizewithattributes:[nsdictionary Dictionarywithobjectsandkeys:fnt,nsfontattributename, Nil]];    The name of h    cgfloat Nameh = size.height;    The name of W    cgfloat Namew = size.width;    _namelabel.frame = CGRectMake (0, 0, Namew,nameh);


IOS7 uses the following method boundingRectWithSize:options:attributes:context: instead of the obsolete iOS6 SizeWithFont:constrainedToSize:lineBreakMode: Method
4, according to the body content how much, dynamically determine the body content frame        //width w    cgfloat contentw = self.bounds.size.width-_content.frame.origin.x- Kmargin;    The font of the label Helveticaneue  Courier    uifont *fnt = [Uifont fontwithname:@ "Helveticaneue" size:18.0f];    _content.font = fnt;    _content.numberoflines = 0;    _content.linebreakmode = nslinebreakbywordwrapping;    IOS7 replaces SizeWithFont:constrainedToSize:lineBreakMode in obsolete iOS6 with the following methods: Method    CGRect tmprect = [_content.text Boundingrectwithsize:cgsizemake (CONTENTW, Options:nsstringdrawinguseslinefragmentorigin) attributes:[ Nsdictionary Dictionarywithobjectsandkeys: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);


Attached: API documentation Reference


BoundingRectWithSize:options:attributes:context:


Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, Withi n 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 being applied to the string. These is the same attributes that can is applied to ansattributedstring object, but in the case ofnsstring< /c1> objects, the attributes apply to the entire string, rather than ranges within the string.

Context

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

Return Value

The bounding rect for the receiver 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 it value to the nearest higher integer using theceil functi On.

This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) is 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 siz E 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.