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

1

15

In CoreText it is easy ask: "for a given rectangle how much of this attributed string will fit?".

CTFrameGetVisibleStringRange(rect).length

Will return where in the string the next run of text should begin.

My question is: "given an attributed string and a width, what rect height do I need to completely bound the attributed string?".

Does the CoreText framework provide tools to do this?

Thanks,
Doug

Waziristan answered 8/8, 2011 at 20:58 Comment(0)
H
22

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);

Howlett answered 10/8, 2011 at 16:27 Comment(2)
Downvoting because this method has never worked (tried in iOS 5 + 6 + 7 - it has major bugs in all iOS versions). The iOS 6 bugs are infamous (Apple spuriously increases width, runs out of space, deletes the last line of height), but it does stupid things like ignoring the glyphs and assuming every character is a "f" or "g", despite having input parameters to prevent exactly that problem.Phylys
this method is widely known to be buggyCloison

© 2022 - 2024 — McMap. All rights reserved.