iOS dynamic fetch NSString string width

Source: Internet
Author: User

When you use Uilabel to store strings, you often need to get the long width data of the label, this article lists some of the commonly used calculation methods.

1. Get the width, get the length required for the string to be displayed in a single line

Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (Maxfloat, 30)];

Note: The width of size should be set to maxfloat if desired.


2. Gets the height, gets the actual height required for the string to wrap within the specified size, which is longer than the label width.

Cgsize titlesize = [astring sizewithfont:font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];

Note: If you want to get a height, the height of size should be set to Maxfloat.


3. In actual programming, it is sometimes necessary to calculate the position of the last character of a piece of text, and then add a picture or other control (such as the info icon), and the following code is a way to calculate the position of the last character in the label.

Cgsize sz = [Label.text sizeWithFont:label.font constrainedtosize:cgsizemake (Maxfloat, 40)];

Cgsize linessz = [Label.text sizeWithFont:label.font constrainedtosize:cgsizemake (label.frame.size.width, Maxfloat) Linebreakmode:uilinebreakmodewordwrap];

if (sz.width <= linessz.width)//Judge if the line is broken

{

Lastpoint = Cgpointmake (label.frame.origin.x + sz.width, LABEL.FRAME.ORIGIN.Y);

}

Else

{

Lastpoint = Cgpointmake (label.frame.origin.x + (int) sz.width% (int) linessz.width,linessz.height-sz.height);

}


ios7.0 after the use of:

CGFloat width1=[(NSString *) ob1 Sizewithfont:[uifont systemfontofsize:16] Constrainedtosize:cgsizemake (1000, Fontheight)].width;
Turn
CGFloat width1=[(NSString *) Ob1 boundingrectwithsize:cgsizemake (1000, fontheight) options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont SystemFontOfSize:16]} Context:nil] . size.width;

Boundingrectwithsize:cgsize corresponds to Constrainedtosize:cgsize
Attributes:@{nsfontattributename: [ uifont ]} corresponds to sizewithfont[ Uifont ]
Options:nsstringdrawinguseslinefragmentorigin Default Photo Fill
Context:nil Default Photo Fill

Turn from: http://www.cnblogs.com/spiritstudio/archive/2011/11/17/2252074.html

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.