[iOS]CTFramesetterSuggestFrameSizeWithConstraints计算文本显示所占区域修正方法
2013-05-14 21:44
1606 查看
(zhuan zi) http://blog.sina.com.cn/s/blog_6308b98c0101byh9.html
[iOS]CTFramesetterSuggestFrameSizeWithConstraints计算文本显示所占区域修正方法
(2012-12-19 15:48:45)![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
转载▼
标签:杂谈 |
UIFont *uiFont = [UIFontfontWithName:@"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 enoughto 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 spacingattributes over the span
CFAttributedStringSetAttribute(string, textRange,kCTFontAttributeName, ctFont);
CFAttributedStringSetAttribute(string, textRange,kCTParagraphStyleAttributeName, paragraphStyle);
CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString(string);
这段代码中,最主要的是对CTFramesetterRef的framesetter进行设置,设置framesetter之后,就可以通过CTFramesetterSuggestFrameSizeWithConstraints来计算文字所占区域大小了。
相关文章推荐
- 计算文本高度 UILabel和sizeWithFont方法
- boundingRectWithSize 7.0以后计算文本高度的方法
- iOS:中计算文本尺寸(boundingRectWithSize)
- iOS 使用 boundingRectWithSize: 计算 UILabel 高度错误的解决方案
- IOS--UITextView 多行文本的输入和显示 的使用方法详细
- 主题 : sizeWithFont iOS 7.0 新方法的写法
- iOS7 中 boundingRectWithSize:options:attributes:context:计算文本尺寸的使用
- UITextView使用 sizeWithFont:constrainedToSize:lineBreakMode:计算自适应文本高度
- CoreText库中使用CTFramesetter,根据文本内容实现丰富的Frame
- IOS7.0 sizeWithFont:constrainedToSize:lineBreakMode:方法
- iOS--UITextView 多行文本的输入和显示 的使用方法详细
- UITextView使用 sizeWithFont:constrainedToSize:lineBreakMode:计算自适应文本高度
- UILable的使用,文本高度的计算boundingRectWithSize
- iOS7 boundingRectWithSize 计算文本尺寸
- 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- ios自定义View:init和initWithFrame方法
- iOS开发笔记 --iOS7 API 对sizeWithFont的替代方法
- 学习boundingRectWithSize:options:attributes:context:计算文本尺寸
- IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度
- IOS 根据 指定的 字体类型 ,size ,计算 绘制 文本 所需要的 宽度与高度