• 计算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;
  • 相关阅读:
    329. Longest Increasing Path in a Matrix
    2、evaluate-reverse-polish-notation
    1、minimum-depth-of-binary-tree
    2、替换空格
    C风格字符串和C++string对象的相互转化
    1、二维数组中的查找
    8、sort排序中比较函数的几种应用方式
    1131(★、※)Subway Map
    7、(★、※)判断一个序列是否是二叉查找树的后序、前序遍历序列
    041219~051219流水账
  • 原文地址:https://www.cnblogs.com/swallow37/p/4184173.html
Copyright © 2020-2023  润新知