zoukankan      html  css  js  c++  java
  • 计算string高度

     一、CTFramesetterSuggestFrameSizeWithConstraints计算文本显示所占区域修正方法
    UIFont    *uiFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CTFontRef ctFont = CTFontCreateWithName((CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
     
    CGFloat leading = uiFont.lineHeight - uiFont.ascender + uiFont.descender;
    CTParagraphStyleSetting paragraphSettings[1] = { kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof (CGFloat), &leading };
     
    CTParagraphStyleRef  paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
    CFRange textRange = CFRangeMake(0, text.length);
     
    //  Create an empty mutable string big enough to hold our test
    CFMutableAttributedStringRef string = CFAttributedStringCreateMutable(kCFAllocatorDefault, text.length);
     
    //  Inject our text into it
    CFAttributedStringReplaceString(string, CFRangeMake(0, 0), (CFStringRef) text);
     
    //  Apply our font and line spacing attributes over the span
    CFAttributedStringSetAttribute(string, textRange, kCTFontAttributeName, ctFont);
    CFAttributedStringSetAttribute(string, textRange, kCTParagraphStyleAttributeName, paragraphStyle);
     
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);

    CFRange range;

    CGSize sizeAfterRender = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, nil,

    CGSizeMake(300, 1000000)

    , &range); 

     二、ios7.0以上方法

    NSDictionary *attributes = @{NSFontAttributeName:font};
    CGSize lablesize = [showtext  boundingRectWithSize:CGSizeMake(300,1000000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading 
    attributes:attributes
    context:nil].size;
  • 相关阅读:
    软件测试理论基础
    使用Pycharm官方统计代码行插件统计代码总行数
    Jmeter的配置文件解析
    python异常整理
    python2与python3的区别
    tomcat的server.xml配置
    异常:Error response from daemon: conflict: unable to delete 6fa48e047721 (cannot be forced)
    前端 -- 定位和z-index
    前端 -- background
    前端 -- 超链接导航栏案例
  • 原文地址:https://www.cnblogs.com/swallow37/p/4184173.html
Copyright © 2011-2022 走看看