Created
July 24, 2012 15:29
-
-
Save wess/3170667 to your computer and use it in GitHub Desktop.
CoreText content Height
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (CGSize)sizeForAttributedStringWithWidth:(CGFloat)width | |
{ | |
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self); | |
CFIndex offset = 0, length; | |
CGFloat y = 0; | |
do { | |
length = CTTypesetterSuggestLineBreak(typesetter, offset, width); | |
CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(offset, length)); | |
CGFloat ascent, descent, leading; | |
CTLineGetTypographicBounds(line, &ascent, &descent, &leading); | |
CFRelease(line); | |
offset += length; | |
y += ascent + descent + leading; | |
} while (offset < [self length]); | |
CFRelease(typesetter); | |
return CGSizeMake(width, ceil(y)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment