Skip to content

Commit adc10ce

Browse files
committed
Font weight option support for iOS (#5490)
* Add fontWeight support on iOS * Add tests * Fix tests
1 parent 3c6314d commit adc10ce

22 files changed

+202
-90
lines changed

docs/docs/styling.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Navigation.mergeOptions(this.props.componentId, {
8282
fontSize: 14,
8383
color: 'red',
8484
fontFamily: 'Helvetica',
85+
fontWeight: 'regular', // Available on iOS only, will ignore fontFamily style and use the iOS system fonts instead. Supported weights are: 'regular', 'bold', 'thin', 'ultraLight', 'light', 'medium', 'semibold', 'heavy' and 'black'.
8586
component: {
8687
name: 'example.CustomTopBarTitle',
8788
alignment: 'center'
@@ -92,6 +93,7 @@ Navigation.mergeOptions(this.props.componentId, {
9293
fontSize: 14,
9394
color: 'red',
9495
fontFamily: 'Helvetica',
96+
fontWeight: 'regular', // Available on iOS only, will ignore fontFamily style and use the iOS system fonts instead. Supported weights are: 'regular', 'bold', 'thin', 'ultraLight', 'light', 'medium', 'semibold', 'heavy' and 'black'.
9597
alignment: 'center'
9698
},
9799
backButton: {
@@ -130,6 +132,7 @@ Navigation.mergeOptions(this.props.componentId, {
130132
textColor: 'red',
131133
selectedTextColor: 'blue',
132134
fontFamily: 'Helvetica',
135+
fontWeight: 'regular', // Available on iOS only, will ignore fontFamily style and use the iOS system fonts instead. Supported weights are: 'regular', 'bold', 'thin', 'ultraLight', 'light', 'medium', 'semibold', 'heavy' and 'black'.
133136
fontSize: 10
134137
},
135138
sideMenu: {
@@ -194,7 +197,8 @@ Navigation.mergeOptions(this.props.componentId, {
194197
visible: true,
195198
fontSize: 30,
196199
color: 'red',
197-
fontFamily: 'Helvetica'
200+
fontFamily: 'Helvetica',
201+
fontWeight: 'regular' // Available on iOS only, will ignore fontFamily style and use the iOS system fonts instead. Supported weights are: 'regular', 'bold', 'thin', 'ultraLight', 'light', 'medium', 'semibold', 'heavy' and 'black'.
198202
},
199203
},
200204
sideMenu: {

docs/docs/topBar-buttons.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ The following options can be used to customise buttons.
1515
color: 'red',
1616
disabledColor: 'black',
1717
testID: 'buttonOneTestID',
18+
fontFamily: 'Helvetica',
19+
fontSize: 10
20+
fontWeight: 'regular', // Available on iOS only, will ignore fontFamily style and use the iOS system fonts instead. Supported weights are: 'regular', 'bold', 'thin', 'ultraLight', 'light', 'medium', 'semibold', 'heavy' and 'black'.
1821
// Android only
1922
showAsAction: 'ifRoom', // See below for details.
2023
// iOS only

lib/ios/RCTConvert+UIFontWeight.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#import <React/RCTConvert.h>
2+
3+
@interface RCTConvert (UIFontWeight)
4+
5+
@end
6+
7+
@implementation RCTConvert (UIFontWeight)
8+
9+
RCT_ENUM_CONVERTER(UIFontWeight,
10+
(@{@"ultraLight": @(UIFontWeightUltraLight),
11+
@"thin": @(UIFontWeightThin),
12+
@"light": @(UIFontWeightLight),
13+
@"regular": @(UIFontWeightRegular),
14+
@"medium": @(UIFontWeightMedium),
15+
@"semibold": @(UIFontWeightSemibold),
16+
@"bold": @(UIFontWeightBold),
17+
@"heavy": @(UIFontWeightHeavy),
18+
@"black": @(UIFontWeightBlack)
19+
}), UIFontWeightRegular, floatValue)
20+
21+
@end

lib/ios/RNNBottomTabOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@property(nonatomic, strong) Color *badgeColor;
1111
@property(nonatomic, strong) DotIndicatorOptions *dotIndicator;
1212
@property(nonatomic, strong) Text *fontFamily;
13+
@property(nonatomic, strong) Text *fontWeight;
1314
@property(nonatomic, strong) Text *testID;
1415
@property(nonatomic, strong) Image *icon;
1516
@property(nonatomic, strong) Image *selectedIcon;

lib/ios/RNNBottomTabOptions.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
1212
self.badge = [TextParser parse:dict key:@"badge"];
1313
self.badgeColor = [ColorParser parse:dict key:@"badgeColor"];
1414
self.fontFamily = [TextParser parse:dict key:@"fontFamily"];
15+
self.fontWeight = [TextParser parse:dict key:@"fontWeight"];
1516
self.testID = [TextParser parse:dict key:@"testID"];
1617

1718
self.dotIndicator = [DotIndicatorParser parse:dict];

lib/ios/RNNFontAttributesCreator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
@interface RNNFontAttributesCreator : NSObject
55

6-
+ (NSDictionary *)createFontAttributesWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize color:(UIColor *)color;
6+
+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor;
7+
8+
+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color;
9+
10+
+ (NSDictionary *)createWithDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor;
711

812
@end

lib/ios/RNNFontAttributesCreator.m

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
#import "RNNFontAttributesCreator.h"
2+
#import "RCTConvert+UIFontWeight.h"
23

34
@implementation RNNFontAttributesCreator
45

5-
+ (NSDictionary *)createFontAttributesWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize color:(UIColor *)color {
6+
+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
67
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary new];
7-
if (fontFamily || fontSize || color) {
8+
return [self createWithDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:defaultFontSize fontWeight:fontWeight color:color defaultColor:defaultColor];
9+
}
10+
11+
+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
12+
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary new];
13+
return [self createWithDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
14+
}
15+
16+
+ (NSDictionary *)createWithDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
17+
return [self createWithDictionary:attributesDictionary fontFamily:fontFamily fontSize:fontSize ?: defaultFontSize fontWeight:fontWeight color:color ?: defaultColor];
18+
}
19+
20+
+ (NSDictionary *)createWithDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
21+
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary dictionaryWithDictionary:attributesDictionary];
22+
if (fontFamily || fontSize || color || fontWeight) {
23+
CGFloat resolvedFontSize = fontSize ? fontSize.floatValue : 17;
824
if (color) {
925
titleTextAttributes[NSForegroundColorAttributeName] = color;
1026
}
11-
if (fontFamily){
12-
if (fontSize) {
13-
titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:[fontSize floatValue]];
14-
} else {
15-
titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:17];
16-
}
17-
} else if (fontSize) {
18-
titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:[fontSize floatValue]];
27+
if (fontWeight) {
28+
titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize weight:[RCTConvert UIFontWeight:fontWeight]];
29+
} else if (fontFamily){
30+
titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:resolvedFontSize];
31+
} else {
32+
titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize];
1933
}
2034
}
2135

lib/ios/RNNLargeTitleOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
@property (nonatomic, strong) Bool* visible;
88
@property (nonatomic, strong) Color* color;
99
@property (nonatomic, strong) Text* fontFamily;
10+
@property (nonatomic, strong) Text* fontWeight;
1011

1112
@end

lib/ios/RNNLargeTitleOptions.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
99
self.visible = [BoolParser parse:dict key:@"visible"];
1010
self.color = [ColorParser parse:dict key:@"color"];
1111
self.fontFamily = [TextParser parse:dict key:@"fontFamily"];
12+
self.fontWeight = [TextParser parse:dict key:@"fontWeight"];
1213

1314
return self;
1415
}

lib/ios/RNNNavigationButtons.m

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import "RNNRootViewController.h"
77
#import "UIImage+insets.h"
88
#import "UIViewController+LayoutProtocol.h"
9+
#import "RNNFontAttributesCreator.h"
910

1011
@interface RNNNavigationButtons()
1112

@@ -126,8 +127,8 @@ -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNBu
126127
BOOL enabledBool = enabled ? [enabled boolValue] : YES;
127128
[barButtonItem setEnabled:enabledBool];
128129

129-
NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
130-
NSMutableDictionary* disabledTextAttributes = [[NSMutableDictionary alloc] init];
130+
NSMutableDictionary* textAttributes = [NSMutableDictionary dictionaryWithDictionary:[RNNFontAttributesCreator createWithFontFamily:dictionary[@"fontFamily"] ?: [defaultStyle.fontFamily getWithDefaultValue:nil] fontSize:dictionary[@"fontSize"] defaultFontSize:[defaultStyle.fontSize getWithDefaultValue:@(17)] fontWeight:dictionary[@"fontWeight"] color:nil defaultColor:nil]];
131+
NSMutableDictionary* disabledTextAttributes = [NSMutableDictionary dictionaryWithDictionary:[RNNFontAttributesCreator createWithFontFamily:dictionary[@"fontFamily"] ?: [defaultStyle.fontFamily getWithDefaultValue:nil] fontSize:dictionary[@"fontSize"] defaultFontSize:[defaultStyle.fontSize getWithDefaultValue:@(17)] fontWeight:dictionary[@"fontWeight"] color:nil defaultColor:nil]];
131132

132133
if (!enabledBool && disabledColor) {
133134
color = disabledColor;
@@ -139,18 +140,7 @@ -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNBu
139140
[barButtonItem setImage:[[iconImage withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
140141
barButtonItem.tintColor = color;
141142
}
142-
143-
NSNumber* fontSize = [self fontSize:dictionary[@"fontSize"] defaultFontSize:[defaultStyle.fontSize getWithDefaultValue:nil]];
144-
NSString* fontFamily = [self fontFamily:dictionary[@"fontFamily"] defaultFontFamily:[defaultStyle.fontFamily getWithDefaultValue:nil]];
145-
UIFont *font = nil;
146-
if (fontFamily) {
147-
font = [UIFont fontWithName:fontFamily size:[fontSize floatValue]];
148-
} else {
149-
font = [UIFont systemFontOfSize:[fontSize floatValue]];
150-
}
151-
[textAttributes setObject:font forKey:NSFontAttributeName];
152-
[disabledTextAttributes setObject:font forKey:NSFontAttributeName];
153-
143+
154144
[barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
155145
[barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
156146
[barButtonItem setTitleTextAttributes:disabledTextAttributes forState:UIControlStateDisabled];
@@ -174,24 +164,6 @@ - (UIColor *)color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
174164
return nil;
175165
}
176166

177-
- (NSNumber *)fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize {
178-
if (fontSize) {
179-
return fontSize;
180-
} else if (defaultFontSize) {
181-
return defaultFontSize;
182-
}
183-
184-
return @(17);
185-
}
186-
187-
- (NSString *)fontFamily:(NSString *)fontFamily defaultFontFamily:(NSString *)defaultFontFamily {
188-
if (fontFamily) {
189-
return fontFamily;
190-
} else {
191-
return defaultFontFamily;
192-
}
193-
}
194-
195167
- (id)getValue:(id)value withDefault:(id)defaultValue {
196168
return value ? value : defaultValue;
197169
}

0 commit comments

Comments
 (0)