30, accurate calculation of coretext height method

Source: Internet
Author: User

http://ios-iphone.diandian.com/post/2012-03-29/18389515

-(int) getattributedstringheightwithstring: (nsattributedstring *) string widthvalue: (int) width
{
int total_height = 0;

Ctframesetterref Framesetter = ctframesettercreatewithattributedstring ((cfattributedstringref) string); String is the nsattributedstring to calculate the height
CGRect drawingrect = CGRectMake (0, 0, width, 1000); The Gaoyao setting here is large enough
Cgmutablepathref path = cgpathcreatemutable ();
Cgpathaddrect (Path, NULL, Drawingrect);
Ctframeref textFrame = Ctframesettercreateframe (Framesetter,cfrangemake (0,0), path, NULL);
Cgpathrelease (path);
Cfrelease (Framesetter);

Nsarray *linesarray = (Nsarray *) ctframegetlines (textFrame);

Cgpoint Origins[[linesarray Count]];
Ctframegetlineorigins (TextFrame, cfrangemake (0, 0), Origins);

int line_y = (int) origins[[linesarray count] -1].y; The y-coordinate of the last line's origin.

CGFloat ascent;
CGFloat descent;
CGFloat leading;

Ctlineref line = (ctlineref) [Linesarray Objectatindex:[linesarray count]-1];
Ctlinegettypographicbounds (line, &ascent, &descent, &leading);

Total_height = 1000-line_y + (int) descent +1; +1 to correct the value of descent converted to int after decimal point

Cfrelease (TextFrame);

return total_height;

}

about line coordinate position y for the black line position descent is the height of the lower part of the black line

About Font Sections Height description http://ios-iphone.diandian.com/post/2012-03-29/18055023

30, accurate calculation of coretext height method

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.