Skip to content

Instantly share code, notes, and snippets.

@darcyliu darcyliu/result.txt

Created Oct 29, 2014
What would you like to do?
text height calculation
size: {102.804, 486.74399999999957}
rect: {{0, 0}, {102.804, 486.74399999999957}}
suggestedSize: {98.6953125, 480}
rect: {{0, 0}, {100, 487}}
NSString *text = @"Wikipedia (Listeni/ˌwɪkɨˈpiːdi.ə/ or Listeni/ˌwɪkiˈpiːdi.ə/ wik-i-pee-dee-ə) is a multilingual, web-based, free-content encyclopedia project supported by the Wikimedia Foundation and based on an openly editable model. The name \"Wikipedia\" is a portmanteau of the words wiki (a technology for creating collaborative websites, from the Hawaiian word wiki, meaning \"quick\") and encyclopedia. Wikipedia's articles provide links designed to guide the user to related pages with additional information.";
UIFont *font = [UIFont systemFontOfSize:12];
CGFloat width = 100;
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)];
NSLog(@"size: %@",NSStringFromCGSize(size));
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
NSLog(@"rect: %@",NSStringFromCGRect(rect));
CFRange fitCFRange = CFRangeMake(0,0);
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedString));
CFDictionaryRef frameAttributes = (__bridge CFDictionaryRef)@{
(id)kCTFrameProgressionAttributeName : @(kCTFrameProgressionTopToBottom),
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,
CFRangeMake(0, attributedString.length),
NSLog(@"suggestedSize: %@",NSStringFromCGSize(suggestedSize));
UILabel *toolLabel = [[UILabel alloc] initWithFrame:CGRectZero];
toolLabel.font = font;
toolLabel.numberOfLines = 0;
toolLabel.adjustsFontSizeToFitWidth = YES;
toolLabel.text = text;
CGRect rect = [toolLabel textRectForBounds:CGRectMake(0, 0, width, CGFLOAT_MAX) limitedToNumberOfLines:0];
NSLog(@"rect: %@",NSStringFromCGRect(rect));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.