Skip to content

Instantly share code, notes, and snippets.

@diegosanchezr
Created March 13, 2015 20:27
Show Gist options
  • Save diegosanchezr/8c740548fd51400ea34c to your computer and use it in GitHub Desktop.
Save diegosanchezr/8c740548fd51400ea34c to your computer and use it in GitHub Desktop.
- (void)stringSizeBug {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 2;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : [UIFont systemFontOfSize:12]
};
NSDictionary *attributes2 = @{
NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : [UIFont systemFontOfSize:12]
};
NSAttributedString *part1 = [[NSAttributedString alloc] initWithString:@"Add me here" attributes:attributes];
NSAttributedString *part2 = [[NSAttributedString alloc] initWithString:@" − Get seen by millions!" attributes:attributes2];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:part1];
[text appendAttributedString:part2];
// Bug 1 in NSAttributedString
NSLog(@"%@", NSStringFromCGSize([text boundingRectWithSize:CGSizeMake(251, 100) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin context:nil].size)); // {201.70800000000003, 16.315999999999999}
NSLog(@"%@", NSStringFromCGSize([text boundingRectWithSize:CGSizeMake(226, 100) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin context:nil].size)); // {201.70800000000003, 14.315999999999999}
// Bug 2 in UILabel
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.attributedText = text;
NSLog(@"%@", NSStringFromCGSize([label sizeThatFits:CGSizeMake(251, 100)])); // {202, 16.5}
NSLog(@"%@", NSStringFromCGSize([label sizeThatFits:CGSizeMake(226, 100)])); // {202, 14.5}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment