首页 > 其他 > 详细

计算string高度

时间:2014-12-25 11:12:57      阅读:243      评论:0      收藏:0      [点我收藏+]
 一、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;

计算string高度

原文:http://www.cnblogs.com/swallow37/p/4184173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!