boundingRectWithSize for NSAttributedString returning wrong size

原创 2015年07月07日 10:18:10


@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 NSObject type (for example, it tried to pass _isDefaultFace to 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);



一,Unknown type name ....    如果是报这个错误,多半是你的对象类型没有被识别,检查是不是没有引用对应的库或者头文件在你的文件头部分,还有可能是循环引用导致的,循环引用的...

IOS8.4下用- (CGRect)boundingRectWithSize: options: attributes: context:计算文本高度会崩溃的问题



在iOS开发中经常需要使用的或不常用的知识点的总结,几年的收藏和积累(踩过的坑)。 一、 iPhone Size 手机型号 屏幕尺寸 iPhone 4 4s 32...

mac OS X10.11.5安装scikit-learn后运行出现ValueError: numpy.dtype has the wrong size的错误

安装机器学习类库 通过pip命令安装机器学习类库 numpy,scipy,scikit-learn sudo pip install numpy scipy scikit-learn 但是安装后,运...

Error解决:Property's synthesized getter follows Cocoa naming convention for returning 'owned'

在项目中定义了以new开头的textField,结果报错: 先看我的源码:#import @interface ResetPasswordViewController : UIViewContr...

iOS编译问题:Property's synthesized getter follows Cocoa naming convention for returning 'owned'

原文链接: 该文章提供了角度比较全面的答案!!! 在项目中定义了以new开头的textFiel...

Oracle for in loop循环时expression is of wrong type报错

1、我本来想做一个嵌套的for in loop循环插入。第一层查出代码,然后利用这个代码再查出所有的日期。但是使用这个日期的时候一直报错,如下:BEGIN FOR item IN (SELEC...

Wrong Image Format for bootm command&ERROR: can't get kernel image

Wrong Image Format for bootm command 如上图所示,这个是在设置环境变量时,因为bootcmd变量有多个命令组成,但很低级的不知道用''包起来导致。 这个错误出现...

A framework for quantifying size dependent

  • 2013年09月15日 11:01
  • 1.07MB
  • 下载
您举报文章:boundingRectWithSize for NSAttributedString returning wrong size