Skip to content

Commit

Permalink
Fix largeTitle behaviour on iOS 13 (#5715)
Browse files Browse the repository at this point in the history
* Fix largeTitle behavior on iOS 13
  • Loading branch information
yogevbd authored Dec 3, 2019
1 parent fddab2c commit 65118b1
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/ios/RNNComponentPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RN
}

- (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
if (!_customTitleView) {
if (!_customTitleView && ![options.topBar.largeTitle.visible getWithDefaultValue:NO]) {
_titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];

if (options.topBar.title.text.hasValue) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ios/RNNFontAttributesCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color;

+ (NSDictionary *)createWithDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor;
+ (NSDictionary *)createFromDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor;

@end
38 changes: 19 additions & 19 deletions lib/ios/RNNFontAttributesCreator.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ @implementation RNNFontAttributesCreator

+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary new];
return [self createWithDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:defaultFontSize fontWeight:fontWeight color:color defaultColor:defaultColor];
return [self createFromDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:defaultFontSize fontWeight:fontWeight color:color defaultColor:defaultColor];
}

+ (NSDictionary *)createWithFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary new];
return [self createWithDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
return [self createFromDictionary:titleTextAttributes fontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
}

+ (NSDictionary *)createWithDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
return [self createWithDictionary:attributesDictionary fontFamily:fontFamily fontSize:fontSize ?: defaultFontSize fontWeight:fontWeight color:color ?: defaultColor];
+ (NSDictionary *)createFromDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
return [self createFromDictionary:attributesDictionary fontFamily:fontFamily fontSize:fontSize ?: defaultFontSize fontWeight:fontWeight color:color ?: defaultColor];
}

+ (NSDictionary *)createWithDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary dictionaryWithDictionary:attributesDictionary];
if (fontFamily || fontSize || color || fontWeight) {
CGFloat resolvedFontSize = fontSize ? fontSize.floatValue : 17;
if (color) {
titleTextAttributes[NSForegroundColorAttributeName] = color;
}
if (fontWeight) {
titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize weight:[RCTConvert UIFontWeight:fontWeight]];
} else if (fontFamily){
titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:resolvedFontSize];
} else {
titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize];
}
}
+ (NSDictionary *)createFromDictionary:(NSDictionary *)attributesDictionary fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
NSMutableDictionary* titleTextAttributes = [NSMutableDictionary dictionaryWithDictionary:attributesDictionary];\
UIFont* currentFont = attributesDictionary[NSFontAttributeName];
CGFloat resolvedFontSize = fontSize ? fontSize.floatValue : currentFont.fontDescriptor.pointSize;
if (color) {
titleTextAttributes[NSForegroundColorAttributeName] = color;
}
if (fontWeight) {
titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:resolvedFontSize weight:[RCTConvert UIFontWeight:fontWeight]];
} else if (fontFamily){
titleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:fontFamily size:resolvedFontSize];
} else if (fontSize) {
titleTextAttributes[NSFontAttributeName] = [UIFont fontWithDescriptor:currentFont.fontDescriptor size:resolvedFontSize];
}

return titleTextAttributes;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ios/RNNTabBarItemCreator.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ + (UIImage *)getIconImage:(UIImage *)icon withTint:(UIColor *)tintColor {
}

+ (void)appendTitleAttributes:(UITabBarItem *)tabItem textColor:(UIColor *)textColor selectedTextColor:(UIColor *)selectedTextColor fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight {
NSDictionary* selectedAttributes = [RNNFontAttributesCreator createWithDictionary:[tabItem titleTextAttributesForState:UIControlStateSelected] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:selectedTextColor defaultColor:[UIColor blackColor]];
NSDictionary* selectedAttributes = [RNNFontAttributesCreator createFromDictionary:[tabItem titleTextAttributesForState:UIControlStateSelected] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:selectedTextColor defaultColor:[UIColor blackColor]];
[tabItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];


NSDictionary* normalAttributes = [RNNFontAttributesCreator createWithDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:textColor defaultColor:[UIColor blackColor]];
NSDictionary* normalAttributes = [RNNFontAttributesCreator createFromDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:textColor defaultColor:[UIColor blackColor]];
[tabItem setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
}

Expand Down
4 changes: 4 additions & 0 deletions lib/ios/UINavigationBar+utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@

- (void)rnn_showBorder:(BOOL)showBorder;

- (void)rnn_setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color;

- (void)rnn_setNavigationBarTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color;

@end
49 changes: 38 additions & 11 deletions lib/ios/UINavigationBar+utils.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "UINavigationBar+utils.h"
#import "RNNFontAttributesCreator.h"


@implementation UINavigationBar (utils)
Expand Down Expand Up @@ -26,6 +27,43 @@ - (void)rnn_setBackgroundColor:(UIColor *)color {
}
}

- (void)rnn_setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
NSDictionary* fontAttributes;
if (@available(iOS 13.0, *)) {
fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.standardAppearance.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
[[self getNavigaitonBarStandardAppearance] setLargeTitleTextAttributes:fontAttributes];
[[self getNavigaitonBarCompactAppearance] setLargeTitleTextAttributes:fontAttributes];
[[self getNavigaitonBarScrollEdgeAppearance] setLargeTitleTextAttributes:fontAttributes];
} else if (@available(iOS 11.0, *)) {
fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
self.largeTitleTextAttributes = fontAttributes;
}
}

- (void)rnn_setNavigationBarTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
NSDictionary* fontAttributes;
if (@available(iOS 13.0, *)) {
fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.standardAppearance.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
[[self getNavigaitonBarStandardAppearance] setTitleTextAttributes:fontAttributes];
[[self getNavigaitonBarCompactAppearance] setTitleTextAttributes:fontAttributes];
[[self getNavigaitonBarScrollEdgeAppearance] setTitleTextAttributes:fontAttributes];
} else if (@available(iOS 11.0, *)) {
fontAttributes = [RNNFontAttributesCreator createFromDictionary:self.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:color defaultColor:nil];
self.titleTextAttributes = fontAttributes;
}
}

- (void)rnn_showBorder:(BOOL)showBorder {
if (@available(iOS 13.0, *)) {
UIColor* shadowColor = showBorder ? [[UINavigationBarAppearance new] shadowColor] : nil;
[[self getNavigaitonBarStandardAppearance] setShadowColor:shadowColor];
[[self getNavigaitonBarCompactAppearance] setShadowColor:shadowColor];
[[self getNavigaitonBarScrollEdgeAppearance] setShadowColor:shadowColor];
} else {
[self setShadowImage:showBorder ? nil : [UIImage new]];
}
}

- (void)setBackgroundColor:(UIColor *)color {
if (@available(iOS 13.0, *)) {
[self getNavigaitonBarStandardAppearance].backgroundColor = color;
Expand Down Expand Up @@ -54,17 +92,6 @@ - (void)setBackgroundColorTransparent {
}
}

- (void)rnn_showBorder:(BOOL)showBorder {
if (@available(iOS 13.0, *)) {
UIColor* shadowColor = showBorder ? [[UINavigationBarAppearance new] shadowColor] : nil;
[[self getNavigaitonBarStandardAppearance] setShadowColor:shadowColor];
[[self getNavigaitonBarCompactAppearance] setShadowColor:shadowColor];
[[self getNavigaitonBarScrollEdgeAppearance] setShadowColor:shadowColor];
} else {
[self setShadowImage:showBorder ? nil : [UIImage new]];
}
}

- (void)configureWithTransparentBackground {
if (@available(iOS 13.0, *)) {
[[self getNavigaitonBarStandardAppearance] configureWithTransparentBackground];
Expand Down
11 changes: 2 additions & 9 deletions lib/ios/UINavigationController+RNNOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ - (void)setBarStyle:(UIBarStyle)barStyle {
}

- (void)setNavigationBarFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];

if (fontAttributes.allKeys.count > 0) {
self.navigationBar.titleTextAttributes = fontAttributes;
}
[self.navigationBar rnn_setNavigationBarTitleFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
}

- (void)setNavigationBarLargeTitleVisible:(BOOL)visible {
Expand All @@ -62,10 +58,7 @@ - (void)setNavigationBarLargeTitleVisible:(BOOL)visible {
}

- (void)setNavigationBarLargeTitleFontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize fontWeight:(NSString *)fontWeight color:(UIColor *)color {
if (@available(iOS 11.0, *)) {
NSDictionary* fontAttributes = [RNNFontAttributesCreator createWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
self.navigationBar.largeTitleTextAttributes = fontAttributes;
}
[self.navigationBar rnn_setNavigationBarLargeTitleFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight color:color];
}

- (void)setNavigationBarTranslucent:(BOOL)translucent {
Expand Down
19 changes: 11 additions & 8 deletions playground/ios/NavigationTests/RNNFontAttributesCreatorTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,41 @@ - (void)testCreateWithFontFamilyWithDefault_shouldCreateDefaultAttributes {
XCTAssertEqual(font.pointSize, defaultFontSize.floatValue);
}

- (void)testCreateWithDictionary_shouldCreateAttributes {
- (void)testcreateFromDictionary_shouldCreateAttributes {
NSString* familyName = @"Helvetica";
NSNumber* fontSize = @(20);
UIColor* fontColor = UIColor.blueColor;

NSDictionary* attributes = [RNNFontAttributesCreator createWithDictionary:@{} fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:fontColor defaultColor:nil];
NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:@{} fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:fontColor defaultColor:nil];
UIFont* font = attributes[NSFontAttributeName];
XCTAssertEqual(attributes[NSForegroundColorAttributeName], fontColor);
XCTAssertTrue([familyName isEqualToString:font.familyName]);
XCTAssertEqual(font.pointSize, fontSize.floatValue);
}

- (void)testCreateWithDictionary_shouldMergeWithDictionary {
- (void)testcreateFromDictionary_shouldMergeWithDictionary {
NSString* familyName = @"Helvetica";
NSNumber* fontSize = @(20);
NSDictionary* dictionary = @{NSForegroundColorAttributeName: UIColor.redColor};

NSDictionary* attributes = [RNNFontAttributesCreator createWithDictionary:dictionary fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:nil defaultColor:nil];
NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:dictionary fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:nil defaultColor:nil];
UIFont* font = attributes[NSFontAttributeName];
XCTAssertEqual(attributes[NSForegroundColorAttributeName], UIColor.redColor);
XCTAssertTrue([familyName isEqualToString:font.familyName]);
XCTAssertEqual(font.pointSize, fontSize.floatValue);
}

- (void)testCreateWithFontFamily_shouldCreateSystemFontWhenOnlySizeAvailable {
- (void)testCreateWithFontFamily_shouldNotChangeFontFamilyWhenOnlySizeAvailable {
NSNumber* fontSize = @(20);
UIFont* initialFont = [UIFont systemFontOfSize:10 weight:UIFontWeightHeavy];
NSMutableDictionary* initialAttributes = [NSMutableDictionary new];
initialAttributes[NSFontAttributeName] = initialFont;

NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:initialAttributes fontFamily:nil fontSize:fontSize defaultFontSize:nil fontWeight:nil color:nil defaultColor:nil];

NSDictionary* attributes = [RNNFontAttributesCreator createWithFontFamily:nil fontSize:fontSize fontWeight:nil color:nil];
UIFont* font = attributes[NSFontAttributeName];
NSString* systemFontFamilyName = [[UIFont systemFontOfSize:20] familyName];
XCTAssertEqual(font.pointSize, fontSize.floatValue);
XCTAssertTrue([font.familyName isEqualToString:systemFontFamilyName]);
XCTAssertTrue([font.familyName isEqualToString:initialFont.familyName]);
}


Expand Down
Loading

0 comments on commit 65118b1

Please sign in to comment.