Calculates the height of a string in a Uilabel in iOS7

Source: Internet
Author: User
Tags uikit

iOS7 calculates the height of a string in Uilabel

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"@implementationUILabel (stringframe)-(cgsize) Boundingrectwithsize: (cgsize) size{nsdictionary*attribute =@{nsfontattributename:self.font}; Cgsize retsize=[Self.text boundingrectwithsize:size Options:nsstri Ngdrawingtruncateslastvisibleline|Nsstringdrawinguseslinefragmentorigin|nsstringdrawingusesfontleading Attributes:attribute        Context:nil].size; returnretsize;}@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:

    //stringNSString *str =@"At Sunset, brew a cup of Camellia, listen to a song mood 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 labelUILabel *label = [UILabelNew]; Label.backgroundcolor=[Uicolor Whitecolor];        [Self.view Addsubview:label]; //label Gets the stringLabel.text =str; //Label Get FontLabel.font = [Uifont fontwithname:nil size: -]; //calculates the size required for a label based on the obtained string and fontCgsize size = [Label Boundingrectwithsize:cgsizemake ( the,0)]; //set infinite line breaksLabel.numberoflines =0; //set the frame of the labelLabel.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< /c4>: astring];

nsrange range = nsmakerange(0, atrstring. Length);

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

    

    size = [astring boundingrectwithsize: Cgsizemake 237 200 )   Options: Nsstringdrawinguseslinefragmentoriginattributes:d ic context: nil

return size;

}

gets the size of the string IOS6

-(cgsize) getstringrect_: (nsstring*) astring

{

cgsize size;

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

size=[astring sizewithfont: Namefont constrainedtosize:cgsizemake(237 , linebreakmode) :nslinebreakbycharwrapping];

return size;

}

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.