zoukankan      html  css  js  c++  java
  • 准确计算CoreText高度的方法:

    - (int)getAttributedStringHeightWithString:(NSAttributedString *)  string  WidthValue:(int) width  
    {  
       int total_height = 0;  
         
       CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);    //string 为要计算高度的NSAttributedString  
       CGRect drawingRect = CGRectMake(0, 0, width, 1000);  //这里的高要设置足够大  
       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;  //最后一行line的原点y坐标  
         
       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为了纠正descent转换成int小数点后舍去的值  
         
       CFRelease(textFrame);  
         
       return total_height;  
         
    }  

    来自:http://blog.csdn.net/iunion/article/details/7925951

  • 相关阅读:
    laravel吐槽系列之一
    每日晨读_20140924
    技术晨读_2014_9_1
    大话胖model和瘦model
    大话PHP缓存头
    vim黏贴自动增加tab的毛病
    Laravel学习
    郑捷2017年电子工业出版社出版的图书《NLP汉语自然语言处理原理与实践》
    delete
    NLP知识结构概述
  • 原文地址:https://www.cnblogs.com/endtel/p/4848325.html
Copyright © 2011-2022 走看看