material-foundation / material-roboto-font-loader-ios

Apache License 2.0
23 stars 18 forks source link

-isLargeForContrastRatios: relies on font name to determine font weight #16

Open Icycle opened 6 years ago

Icycle commented 6 years ago

The UIFont interface doesn't expose any methods to access the weight property of a font. The current implementation of -isLargeForContrastRatios: wants to treat medium weight fonts as bold, and it looks in the font name for the substring "medium". This is somewhat brittle.

The CoreText font interface does expose the weight property, and since this class is toll-free bridged to UIFont, the implementation ought to use CoreText to inspect the true weight attribute, something like this:

CTFontRef coreTextFont = (bridge CTFontRef)font; CFDictionaryRef coreTextFontTraits = CTFontCopyTraits(coreTextFont); NSDictionary *fontTraits = (bridge NSDictionary )coreTextFontTraits; NSNumber fontWeight = fontTraits[(NSString *)kCTFontWeightTrait]; CFRelease(coreTextFontTraits);

if (fontWeight.floatValue >= (UIFontWeightMedium - FLT_EPSILON)) { return YES; }