Calculates the height of a string in a Uilabel in iOS7

Source: Internet
Author: User

Calculates the height of a string in a Uilabel in iOS7

A new method for calculating the framesize of Uilabel in Uilabel based on a given font and STR is presented in iOS7. I provide the following category:

Uilabel+stringframe.h

  uilabel+stringframe.h//  labelheight////  Copyright (c) 2014 y.x. All rights reserved.//#import <UIKit/UIKit.h> @interface UILabel (stringframe)-(Cgsize) Boundingrectwithsize: ( cgsize) size; @end

Uilabel+stringframe.m

  uilabel+stringframe.m//  labelheight////  Copyright (c) 2014 y.x. All rights reserved.//#import "Uilabel+stringframe.h" @implementation UILabel (Stringframe)-(cgsize) Boundingrectwithsize: (cgsize) size{    nsdictionary *attribute = @{nsfontattributename:self.font};        Cgsize retsize = [self.text boundingrectwithsize:size                                             options:                      nsstringdrawingtruncateslastvisibleline |                      Nsstringdrawinguseslinefragmentorigin |                      Nsstringdrawingusesfontleading                                          attributes:attribute                                             context:nil].size;        return retsize;} @end

Now to test the code:

As follows:

Let's try it. Use a custom font test once:

Show Results:

Both custom fonts and system fonts can be used perfectly.

The following code is for you to test:

    String    nsstring *str = @ "At sunset, brew a cup of Camellia, listen to a song artistic conception empty Far" Zen ", mind with this nature, indulge in mysterious fantasy. As if I was that shuttle in luxuriantly forest in a bird, sometimes circling shuttle, sometimes sing; as if I was that gurgling pouring in a mountain stream of a clear spring, ripples light, Hao Miao long flow; as if I was Nagan in the heaven and earth between a hill, stalwart towering, calmly stretching. I do not believe in Buddha, just like the ethereal sound of the Vatican, and the mercy of the Buddhist meditation, in the Qing Huan, calmly quiet, comfortable Enron. has been longing to go into the green Mountains, blue water, understanding the beauty of the landscape, appreciate the rise and fall of vegetation, and listen to the sound of Huang Tian thick earth, to explore the natural wok too of the universe. Into the landscape, also out of the noise, to the body and mind to cool, to the spirit to precipitate, to the soul to sublimate. ";        Initialize label    UILabel *label = [UILabel new];    Label.backgroundcolor = [Uicolor whitecolor];    [Self.view Addsubview:label];        Label gets the string    label.text = str;        Label gets the font    label.font = [Uifont fontwithname:nil size:18];        Calculates the size    cgsize size = [Label boundingrectwithsize:cgsizemake (0)]        based on the obtained string and the font that the label needs. Set infinite line break    label.numberoflines = 0;        Set the label frame    label.frame = CGRectMake (0.0f, 50.0f, Size.width, size.height);

IOS6 can use the following method to get the size value of a string

size = [Text Sizewithfont:font
Constrainedtosize:size
Linebreakmode:nslinebreakbycharwrapping];

But this method has been abandoned in the iOS7, note.

Gets the size of the string iOS7

-(Cgsize) Getstringrect: (nsstring*) astring

{

Cgsize size;

nsattributedstring* atrstring = [[Nsattributedstringalloc] initwithstring:astring];

Nsrange range = nsmakerange (0, atrstring.length);

nsdictionary* dic = [atrstring attributesatindex:0 effectiverange:&range];

size = [astring boundingrectwithsize:cgsizemake (237, $) Options:nsstringdrawinguseslinefragmentoriginattributes: Diccontext:nil].size;

return size;

}

Gets the size of the string IOS6

-(Cgsize) Getstringrect_: (nsstring*) astring

{

Cgsize size;

Uifont *namefont=[uifont fontwithname:@ "Helvetica" size:13];

Size=[astring Sizewithfont:namefont constrainedtosize:cgsizemake (237, linebreakmode:nslinebreakbycharwrapping) ];

return size;

}

Original: http://www.cnblogs.com/YouXianMing/p/3706269.html

Calculates the height of a string in a Uilabel in iOS7

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.