Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
UIFont.leading is broken in iOS 8. Here's how you workaround it.
iOS 8
2016-07-13 22:49:18.634 leading_broken[266:12919] font <UICTFont: 0x14f560af0> font-family: "HiraKakuProN-W6"; font-weight: bold; font-style: normal; font-size: 21.00pt
2016-07-13 22:49:18.635 leading_broken[266:12919] font pointSize 21.000000
2016-07-13 22:49:18.635 leading_broken[266:12919] font lineHeight 21.000000
2016-07-13 22:49:18.635 leading_broken[266:12919] font ascender 18.480000
2016-07-13 22:49:18.635 leading_broken[266:12919] font descender -2.520000
2016-07-13 22:49:18.636 leading_broken[266:12919] font leading 21.000000
2016-07-13 22:49:18.636 leading_broken[266:12919] ctfont pointSize 21.000000
2016-07-13 22:49:18.636 leading_broken[266:12919] ctfont boundingBox {{-9.072000000000001, -10.038}, {37.590000000000003, 37.590000000000003}}
2016-07-13 22:49:18.636 leading_broken[266:12919] ctfont ascender 18.480000
2016-07-13 22:49:18.636 leading_broken[266:12919] ctfont descender 2.520000
2016-07-13 22:49:18.637 leading_broken[266:12919] ctfont leading 10.500000
2016-07-13 22:49:18.638 leading_broken[266:12919] font <UICTFont: 0x14f523050> font-family: "HiraKakuProN-W3"; font-weight: normal; font-style: normal; font-size: 16.00pt
2016-07-13 22:49:18.638 leading_broken[266:12919] font pointSize 16.000000
2016-07-13 22:49:18.639 leading_broken[266:12919] font lineHeight 16.000000
2016-07-13 22:49:18.639 leading_broken[266:12919] font ascender 14.080000
2016-07-13 22:49:18.639 leading_broken[266:12919] font descender -1.920000
2016-07-13 22:49:18.639 leading_broken[266:12919] font leading 16.000000
2016-07-13 22:49:18.640 leading_broken[266:12919] ctfont pointSize 16.000000
2016-07-13 22:49:18.640 leading_broken[266:12919] ctfont boundingBox {{-6.6719999999999997, -6.4640000000000004}, {26.880000000000003, 27.216000000000001}}
2016-07-13 22:49:18.640 leading_broken[266:12919] ctfont ascender 14.080000
2016-07-13 22:49:18.641 leading_broken[266:12919] ctfont descender 1.920000
2016-07-13 22:49:18.641 leading_broken[266:12919] ctfont leading 8.000000
2016-07-13 22:50:21.908 leading_broken[271:13384] font <UICTFont: 0x14f5212030> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt
2016-07-13 22:50:21.908 leading_broken[271:13384] font leading 12.000000
2016-07-13 22:50:21.908 leading_broken[271:13384] ctfont pointSize 12.000000
2016-07-13 22:50:21.908 leading_broken[271:13384] ctfont boundingBox {{-11.408203125, -5.771484375}, {28.7578125, 19.23046875}}
2016-07-13 22:50:21.908 leading_broken[271:13384] ctfont ascender 9.240234
2016-07-13 22:50:21.908 leading_broken[271:13384] ctfont descender 2.759766
2016-07-13 22:50:21.908 leading_broken[271:13384] ctfont leading 0.000000
==========
iOS 9
2016-07-13 22:53:47.244 leading_broken[9411:1960337] font <UICTFont: 0x14530d60> font-family: "HiraKakuProN-W6"; font-weight: bold; font-style: normal; font-size: 21.00pt
2016-07-13 22:53:47.244 leading_broken[9411:1960337] font pointSize 21.000000
2016-07-13 22:53:47.244 leading_broken[9411:1960337] font lineHeight 21.000000
2016-07-13 22:53:47.245 leading_broken[9411:1960337] font ascender 18.480000
2016-07-13 22:53:47.245 leading_broken[9411:1960337] font descender -2.520000
2016-07-13 22:53:47.245 leading_broken[9411:1960337] font leading 10.500000
2016-07-13 22:53:47.245 leading_broken[9411:1960337] ctfont pointSize 21.000000
2016-07-13 22:53:47.245 leading_broken[9411:1960337] ctfont boundingBox {{-8.4209995, -9.3870001}, {36.287998, 36.287998}}
2016-07-13 22:53:47.246 leading_broken[9411:1960337] ctfont ascender 18.480000
2016-07-13 22:53:47.246 leading_broken[9411:1960337] ctfont descender 2.520000
2016-07-13 22:53:47.246 leading_broken[9411:1960337] ctfont leading 10.500000
2016-07-13 22:53:47.248 leading_broken[9411:1960337] font <UICTFont: 0x15b86f90> font-family: "HiraKakuProN-W3"; font-weight: normal; font-style: normal; font-size: 16.00pt
2016-07-13 22:53:47.249 leading_broken[9411:1960337] font pointSize 16.000000
2016-07-13 22:53:47.249 leading_broken[9411:1960337] font lineHeight 16.000000
2016-07-13 22:53:47.249 leading_broken[9411:1960337] font ascender 14.080001
2016-07-13 22:53:47.249 leading_broken[9411:1960337] font descender -1.920000
2016-07-13 22:53:47.249 leading_broken[9411:1960337] font leading 8.000000
2016-07-13 22:53:47.250 leading_broken[9411:1960337] ctfont pointSize 16.000000
2016-07-13 22:53:47.250 leading_broken[9411:1960337] ctfont boundingBox {{-6.1760001, -5.9680004}, {25.888, 26.224001}}
2016-07-13 22:53:47.252 leading_broken[9411:1960337] ctfont ascender 14.080001
2016-07-13 22:53:47.252 leading_broken[9411:1960337] ctfont descender 1.920000
2016-07-13 22:53:47.253 leading_broken[9411:1960337] ctfont leading 8.000000
NSLog(@"font %@", font);
NSLog(@"font pointSize %lf", font.pointSize);
NSLog(@"font lineHeight %lf", font.lineHeight);
NSLog(@"font ascender %lf", font.ascender);
NSLog(@"font descender %lf", font.descender);
NSLog(@"font leading %lf", font.leading);
//CTFontRef ctfont = CTFontCreateWithName(CFSTR("Helvetica"), 12.0, NULL);
CTFontRef ctfont = CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL);
NSLog(@"ctfont pointSize %lf", CTFontGetSize(ctfont));
NSLog(@"ctfont boundingBox %@", NSStringFromCGRect(CTFontGetBoundingBox(ctfont)));
NSLog(@"ctfont ascender %lf", CTFontGetAscent(ctfont));
NSLog(@"ctfont descender %lf", CTFontGetDescent(ctfont));
NSLog(@"ctfont leading %lf", CTFontGetLeading(ctfont));
CFRelease(ctfont);
- (CGFloat)leading8Compatible
{
if (SYSTEM_VERSION_LESS_THAN(@"9")) {
// iOS 8 or earlier (it's broken, we gotta use CTFont to workaround)
CTFontRef ctfont = CTFontCreateWithName((__bridge CFStringRef)self.fontName, self.pointSize, NULL);
CGFloat leading = CTFontGetLeading(ctfont);
CFRelease(ctfont);
return leading;
} else {
// iOS 9 or later (it's fixed!)
return self.leading;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment