CoreText. How Do I Calculate the Bounding Box of an Attributed String?

穿精又带淫゛_ 提交于 2019-11-30 03:40:25
Joshua

What you need is CTFramesetterSuggestFrameSizeWithConstraints(), you can use it like so:

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedString)); /*Create your framesetter based in you NSAttrinbutedString*/
CGFloat widthConstraint = 500; // Your width constraint, using 500 as an example
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(
   framesetter, /* Framesetter */
   CFRangeMake(0, text.length), /* String range (entire string) */
   NULL, /* Frame attributes */
   CGSizeMake(widthConstraint, CGFLOAT_MAX), /* Constraints (CGFLOAT_MAX indicates unconstrained) */
   NULL /* Gives the range of string that fits into the constraints, doesn't matter in your situation */
);
CGFloat suggestedHeight = suggestedSize.height;

EDIT

//IMPORTANT: Release the framesetter, even with ARC enabled!
CFRelease(frameSetter);

As ARC releases only Objective-C objects, and CoreText deals with C, very likely you can have a memory leak here. If your NSAttributedString is small and you do it once, you shouldn't have any bad consequences. But in a case you have a loop to calculate, let's say, 50 heights of big/complex NSAttributedStrings, and you don't release the CTFramesetterRef, you can have serious memory leaks. Check the tutorial linked for more information on memory leaks and debugging with instruments.

So the solution for this problem is to add CFRelease(frameSetter);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!