Skip to content

Instantly share code, notes, and snippets.

@wess
Created July 24, 2012 15:29
Show Gist options
  • Save wess/3170667 to your computer and use it in GitHub Desktop.
Save wess/3170667 to your computer and use it in GitHub Desktop.
CoreText content Height
- (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