[Typography] Match large font family on iOS 9+
* Use the same font family when loading fonts as iOS does by default.
Resolves
* Format test.
* Format test.
* Updated comment.
MODIFY
components/Typography/src/UIFontDescriptor+MaterialTypography.m
components/Typography/src/UIFontDescriptor+MaterialTypography.m
// Store the system font family name to ensure that we load the system font.
// If we do not explicitly include this UIFontDescriptorFamilyAttribute in the
- // FontDescriptor the OS will default to Helvetica.
- static NSString *systemFontFamilyName;
+ // FontDescriptor the OS will default to Helvetica. On iOS 9+, the Font Family
+ // changes from San Francisco to San Francisco Display at point size 20.
+ static NSString *smallSystemFontFamilyName;
+ static NSString *largeSystemFontFamilyName;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
- UIFont *systemFont;
+ UIFont *smallSystemFont;
+ UIFont *largeSystemFont;
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
- systemFont = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
+ smallSystemFont = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
+ largeSystemFont = [UIFont systemFontOfSize:20 weight:UIFontWeightRegular];
} else {
// TODO: Remove this fallback once we are 8.2+
- systemFont = [UIFont systemFontOfSize:12];
+ smallSystemFont = [UIFont systemFontOfSize:12];
+ largeSystemFont = [UIFont systemFontOfSize:20];
}
- systemFontFamilyName = [systemFont.familyName copy];
+ smallSystemFontFamilyName = [smallSystemFont.familyName copy];
+ largeSystemFontFamilyName = [largeSystemFont.familyName copy];
});
NSDictionary *traits = @{ UIFontWeightTrait : @(materialTraits.weight) };
+ NSString *fontFamily =
+ materialTraits.pointSize < 19.5 ? smallSystemFontFamilyName : largeSystemFontFamilyName;
NSDictionary *attributes = @{
UIFontDescriptorSizeAttribute : @(materialTraits.pointSize),
UIFontDescriptorTraitsAttribute : traits,
- UIFontDescriptorFamilyAttribute : systemFontFamilyName
+ UIFontDescriptorFamilyAttribute : fontFamily
};
UIFontDescriptor *fontDescriptor = [[UIFontDescriptor alloc] initWithFontAttributes:attributes];

MODIFY
components/Typography/tests/unit/TypographyTests.m
components/Typography/tests/unit/TypographyTests.m
@@ -330,4 +330,32 @@ static const CGFloat kOpacityMedium = 0.87f;
// Cleanup
[MDCTypography setFontLoader:[[MDCSystemFontLoader alloc] init]];
}
+
+- (void)testFontFamilyMatchesSystemFontFamily {
+ // Given
+ NSArray<NSNumber *> *allFontStyles = @[
+ @(MDCFontTextStyleBody1),
+ @(MDCFontTextStyleBody2),
+ @(MDCFontTextStyleCaption),
+ @(MDCFontTextStyleHeadline),
+ @(MDCFontTextStyleSubheadline),
+ @(MDCFontTextStyleTitle),
+ @(MDCFontTextStyleDisplay1),
+ @(MDCFontTextStyleDisplay2),
+ @(MDCFontTextStyleDisplay3),
+ @(MDCFontTextStyleDisplay4),
+ @(MDCFontTextStyleButton),
+ ];
+
+ for (NSNumber *styleObject in allFontStyles) {
+ // When
+ MDCFontTextStyle style = styleObject.integerValue;
+ UIFont *mdcFont = [UIFont mdc_preferredFontForMaterialTextStyle:style];
+ UIFont *systemFont = [UIFont systemFontOfSize:mdcFont.pointSize weight:UIFontWeightRegular];
+
+ // Then
+ XCTAssertEqualObjects(systemFont.familyName, mdcFont.familyName);
+ }
+}
+
@end

[Typography] Match large font family on iOS 9+ (#1880)

* Use the same font family when loading fonts as iOS does by default.

Resolves #1805.

* Format test.

* Format test.

* Updated comment.