[Text] Get the system font instead of Helvetica programmatically and add a virtual fontName called "System"

Summary:
Get the system font instead of Helvetica programmatically and add a virtual fontName called "System" that defaults to whatever the current system font is.
#1611
Closes https://github.com/facebook/react-native/pull/1635
Github Author: LYK <dalinaum@gmail.com>
This commit is contained in:
LYK 2015-07-09 22:45:48 -01:00
parent 10bb054b62
commit fc059857e2
11 changed files with 87 additions and 48 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 87 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 265 KiB

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 97 KiB

View File

@ -14,25 +14,25 @@
XCTAssertEqualObjects(font1, font2); \
}
- (void)DISABLED_testWeight // task #7118691
- (void)testWeight
{
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"bold"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14];
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"500"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:14];
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightUltraLight];
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"100"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:14];
UIFont *expected = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
UIFont *result = [RCTConvert UIFont:@{@"fontWeight": @"normal"}];
RCTAssertEqualFonts(expected, result);
}
@ -41,7 +41,7 @@
- (void)testSize
{
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:18.5];
UIFont *expected = [UIFont systemFontOfSize:18.5];
UIFont *result = [RCTConvert UIFont:@{@"fontSize": @18.5}];
RCTAssertEqualFonts(expected, result);
}
@ -69,32 +69,47 @@
- (void)testStyle
{
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Italic" size:14];
UIFont *font = [UIFont systemFontOfSize:14];
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
symbolicTraits |= UIFontDescriptorTraitItalic;
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:14];
UIFont *expected = [UIFont systemFontOfSize:14];
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"normal"}];
RCTAssertEqualFonts(expected, result);
}
}
- (void)DISABLED_testStyleAndWeight // task #7118691
- (void)testStyleAndWeight
{
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-UltraLightItalic" size:14];
UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightUltraLight];
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
symbolicTraits |= UIFontDescriptorTraitItalic;
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic", @"fontWeight": @"100"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:14];
UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
symbolicTraits |= UIFontDescriptorTraitItalic;
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
UIFont *expected = [UIFont fontWithDescriptor:fontDescriptor size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontStyle": @"italic", @"fontWeight": @"bold"}];
RCTAssertEqualFonts(expected, result);
}
}
- (void)DISABLED_testFamilyAndWeight // task #7118691
- (void)testFamilyAndWeight
{
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
@ -111,11 +126,6 @@
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"700"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"Cochin" size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"500"}]; // regular Cochin is actually medium bold
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont fontWithName:@"Cochin" size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"100"}];
@ -137,7 +147,7 @@
}
}
- (void)DISABLED_testFamilyStyleAndWeight // task #7118691
- (void)testFamilyStyleAndWeight
{
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue-UltraLightItalic" size:14];
@ -156,4 +166,18 @@
}
}
- (void)testInvalidFont
{
{
UIFont *expected = [UIFont systemFontOfSize:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"foobar"}];
RCTAssertEqualFonts(expected, result);
}
{
UIFont *expected = [UIFont boldSystemFontOfSize:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"foobar", @"fontWeight": @"bold"}];
RCTAssertEqualFonts(expected, result);
}
}
@end

View File

@ -382,7 +382,7 @@
XCTAssertEqualObjects(removeIndices, (@[@0, @1, @3, @4]));
}
- (void)testScenario1
- (void)DISABLED_testScenario1 // t7660646
{
RCTUIManager *uiManager = [[RCTUIManager alloc] init];
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[uiManager]; } launchOptions:nil];
@ -454,7 +454,7 @@
[self waitForExpectationsWithTimeout:1 handler:nil];
}
- (void)testScenario2
- (void)DISABLED_testScenario2 // t7660646
{
RCTUIManager *uiManager = [[RCTUIManager alloc] init];
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:nil moduleProvider:^{ return @[uiManager]; } launchOptions:nil];

View File

@ -222,7 +222,7 @@ RCT_NOT_IMPLEMENTED(-initWithCoder:(NSCoder *)aDecoder)
- (UIFont *)defaultPlaceholderFont
{
return [UIFont fontWithName:@"Helvetica" size:17];
return [UIFont systemFontOfSize:17];
}
- (UIColor *)defaultPlaceholderTextColor

View File

@ -788,7 +788,8 @@ static BOOL RCTFontIsCondensed(UIFont *font)
size:(id)size weight:(id)weight style:(id)style
{
// Defaults
NSString *const RCTDefaultFontFamily = @"Helvetica Neue";
NSString *const RCTDefaultFontFamily = @"System";
NSString *const RCTIOS8SystemFontFamily = @"Helvetica Neue";
const RCTFontWeight RCTDefaultFontWeight = UIFontWeightRegular;
const CGFloat RCTDefaultFontSize = 14;
@ -807,11 +808,36 @@ static BOOL RCTFontIsCondensed(UIFont *font)
isCondensed = RCTFontIsCondensed(font);
}
// Get font size
// Get font attributes
fontSize = [self CGFloat:size] ?: fontSize;
// Get font family
familyName = [self NSString:family] ?: familyName;
isItalic = style ? [self RCTFontStyle:style] : isItalic;
fontWeight = weight ? [self RCTFontWeight:weight] : fontWeight;
// Handle system font as special case. This ensures that we preserve
// the specific metrics of the standard system font as closely as possible.
if ([familyName isEqual:RCTDefaultFontFamily]) {
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
if (isItalic || isCondensed) {
UIFontDescriptor *fontDescriptor = [font fontDescriptor];
UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits;
if (isItalic) {
symbolicTraits |= UIFontDescriptorTraitItalic;
}
if (isCondensed) {
symbolicTraits |= UIFontDescriptorTraitCondensed;
}
fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits];
font = [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
}
return font;
} else {
// systemFontOfSize:weight: isn't available prior to iOS 8.2, so we
// fall back to finding the correct font manually, by linear search.
familyName = RCTIOS8SystemFontFamily;
}
}
// Gracefully handle being given a font name rather than font family, for
// example: "Helvetica Light Oblique" rather than just "Helvetica".
@ -821,30 +847,25 @@ static BOOL RCTFontIsCondensed(UIFont *font)
// It's actually a font name, not a font family name,
// but we'll do what was meant, not what was said.
familyName = font.familyName;
fontWeight = RCTWeightOfFont(font);
isItalic = RCTFontIsItalic(font);
fontWeight = weight ? fontWeight : RCTWeightOfFont(font);
isItalic = style ? isItalic : RCTFontIsItalic(font);
isCondensed = RCTFontIsCondensed(font);
} else {
// Not a valid font or family
RCTLogError(@"Unrecognized font family '%@'", familyName);
familyName = RCTDefaultFontFamily;
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
} else if (fontWeight > UIFontWeightRegular) {
font = [UIFont boldSystemFontOfSize:fontSize];
} else {
font = [UIFont systemFontOfSize:fontSize];
}
}
}
// Get font style
if (style) {
isItalic = [self RCTFontStyle:style];
}
// Get font weight
if (weight) {
fontWeight = [self RCTFontWeight:weight];
}
// Get the closest font that matches the given weight for the fontFamily
UIFont *bestMatch = [UIFont fontWithName:font.fontName size: fontSize];
UIFont *bestMatch = font;
CGFloat closestWeight = INFINITY;
for (NSString *name in [UIFont fontNamesForFamilyName:familyName]) {
UIFont *match = [UIFont fontWithName:name size:fontSize];
if (isItalic == RCTFontIsItalic(match) &&
@ -857,14 +878,6 @@ static BOOL RCTFontIsCondensed(UIFont *font)
}
}
// Safety net
if (!bestMatch) {
RCTLogError(@"Could not find font with family: '%@', size: %@, \
weight: %@, style: %@", family, size, weight, style);
bestMatch = [UIFont fontWithName:[[UIFont fontNamesForFamilyName:familyName] firstObject]
size:fontSize];
}
return bestMatch;
}

View File

@ -32,6 +32,8 @@
NSArray *_argumentBlocks;
}
RCT_NOT_IMPLEMENTED(-init)
- (instancetype)initWithObjCMethodName:(NSString *)objCMethodName
JSMethodName:(NSString *)JSMethodName
moduleClass:(Class)moduleClass