boundingRectWithSize for NSAttributedString returning wrong size
2015-07-07 10:18
148 查看
https://stackoverflow.com/questions/13621084/boundingrectwithsize-for-nsattributedstring-returning-wrong-size
有个哥们儿的coretext方法非常好,解决了我的这个问题
@warrenm :
That method seems buggy in numerous ways. For one, as you note, it doesn't respect width constraints. For another, I've seen it crash because it seems to assume that all attributes are of
(for example, it tried to pass
a
It will also crash sometimes when a string drawing context is provided because it tries to add a nil-valued attribute to a mutable attributed string behind the scenes.
I would encourage you to avoid this method entirely. You can use Core Text directly to estimate the string size, if you can handle the overhead of creating a framesetter for each string you need to draw. It doesn't precisely honor width constraints either,
but it seems to get within a few pixels, in my experience.
有个哥们儿的coretext方法非常好,解决了我的这个问题
@warrenm :
That method seems buggy in numerous ways. For one, as you note, it doesn't respect width constraints. For another, I've seen it crash because it seems to assume that all attributes are of
NSObjecttype
(for example, it tried to pass
_isDefaultFaceto
a
CTFontRef).
It will also crash sometimes when a string drawing context is provided because it tries to add a nil-valued attribute to a mutable attributed string behind the scenes.
I would encourage you to avoid this method entirely. You can use Core Text directly to estimate the string size, if you can handle the overhead of creating a framesetter for each string you need to draw. It doesn't precisely honor width constraints either,
but it seems to get within a few pixels, in my experience.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attrString); CGSize targetSize = CGSizeMake(320, CGFLOAT_MAX); CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [attrString length]), NULL, targetSize, NULL); CFRelease(framesetter);
相关文章推荐
- Xcode - 报错/技巧 - 在编译的时候,顺便检查内存泄露
- unordered_set介绍
- ClustrMaps——访问者位置
- Spring4 与 Hibernate4 整合过程中的问题记录
- ajax中文传送到模板显示为null
- Nosql(hbase)与传统数据库的区别
- 7月4日全球域名注册商(国际域名)保有量及市场份额
- Oracle使用存储过程下载Blob大对象
- [leetcode][list] Remove Duplicates from Sorted List
- 定义类型类
- Regular Expression Matching 递归
- 32、du命令
- Spring 使用注解方式进行事务管理
- 云服务器使用帮助
- SQL连接查询深度探险
- Could not connect to Redis at XX.XX.XX.XX:XX: Cannot assign requested address
- String c = new String("xyz")会创建几个String对象的问题
- SqlServer中查看当前Open的Cursor方法
- 社説 20150707 なでしこ準優勝 ブームから「文化」への契機に
- linux程序设计——取消一个线程(第十二章)