Skip to content

Commit

Permalink
Back button font family and font size (#5957)
Browse files Browse the repository at this point in the history
* adding fontFamily to backButton Object

* hiding font button for android

* remove podfile.lock

* refactor uicontrolstate

* resolve backButton mergeOptions withDefault

* Add fontSize support

* Reset Podfile.lock

* Update OptionsScreen.js

* Update Options.ts

Co-authored-by: Andy <53788738+Andy40au@users.noreply.github.com>
Co-authored-by: Guy Carmeli <guyca@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 25, 2020
1 parent 79ad642 commit b438588
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/ios/RNNBackButtonOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

@property (nonatomic, strong) Image* icon;
@property (nonatomic, strong) Text* title;
@property (nonatomic, strong) Text* fontFamily;
@property (nonatomic, strong) Number* fontSize;
@property (nonatomic, strong) Text* transition;
@property (nonatomic, strong) Color* color;
@property (nonatomic, strong) Bool* showTitle;
Expand Down
32 changes: 18 additions & 14 deletions lib/ios/RNNBackButtonOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
@implementation RNNBackButtonOptions

- (instancetype)initWithDict:(NSDictionary *)dict {
self = [super init];

self.icon = [ImageParser parse:dict key:@"icon"];
self.title = [TextParser parse:dict key:@"title"];
self.transition = [TextParser parse:dict key:@"transition"];
self.color = [ColorParser parse:dict key:@"color"];
self.showTitle = [BoolParser parse:dict key:@"showTitle"];
self.visible = [BoolParser parse:dict key:@"visible"];

return self;
self = [super init];

self.icon = [ImageParser parse:dict key:@"icon"];
self.title = [TextParser parse:dict key:@"title"];
self.transition = [TextParser parse:dict key:@"transition"];
self.color = [ColorParser parse:dict key:@"color"];
self.showTitle = [BoolParser parse:dict key:@"showTitle"];
self.visible = [BoolParser parse:dict key:@"visible"];
self.fontFamily = [TextParser parse:dict key:@"fontFamily"];
self.fontSize = [NumberParser parse:dict key:@"fontSize"];

return self;
}

- (BOOL)hasValue {
return self.icon.hasValue ||
self.showTitle.hasValue ||
self.color.hasValue ||
self.title.hasValue;
return self.icon.hasValue ||
self.showTitle.hasValue ||
self.color.hasValue ||
self.fontFamily.hasValue ||
self.fontSize.hasValue ||
self.title.hasValue;
}


Expand Down
18 changes: 12 additions & 6 deletions lib/ios/TopBarPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (void)applyOptions:(RNNTopBarOptions *)options {
[self setTitleAttributes:options.title];
[self setLargeTitleAttributes:options.largeTitle];
[self showBorder:![options.noBorder getWithDefaultValue:NO]];
[self setBackButtonIcon:[options.backButton.icon getWithDefaultValue:nil] withColor:[options.backButton.color getWithDefaultValue:nil] title:[options.backButton.title getWithDefaultValue:nil] showTitle:[options.backButton.showTitle getWithDefaultValue:YES]];
[self setBackButtonOptions:[options.backButton.icon getWithDefaultValue:nil] withColor:[options.backButton.color getWithDefaultValue:nil] title:[options.backButton.title getWithDefaultValue:nil] showTitle:[options.backButton.showTitle getWithDefaultValue:YES] fontFamily:[options.backButton.fontFamily getWithDefaultValue:nil] fontSize:[options.backButton.fontSize getWithDefaultValue:nil]];
}

- (void)applyOptionsBeforePopping:(RNNTopBarOptions *)options {
Expand Down Expand Up @@ -48,7 +48,7 @@ - (void)mergeOptions:(RNNTopBarOptions *)options withDefault:(RNNTopBarOptions *
}

if (options.backButton.hasValue) {
[self setBackButtonIcon:[withDefault.backButton.icon getWithDefaultValue:nil] withColor:[withDefault.backButton.color getWithDefaultValue:nil] title:[withDefault.backButton.title getWithDefaultValue:nil] showTitle:[withDefault.backButton.showTitle getWithDefaultValue:YES]];
[self setBackButtonOptions:[withDefault.backButton.icon getWithDefaultValue:nil] withColor:[withDefault.backButton.color getWithDefaultValue:nil] title:[withDefault.backButton.title getWithDefaultValue:nil] showTitle:[withDefault.backButton.showTitle getWithDefaultValue:YES] fontFamily:[withDefault.backButton.fontFamily getWithDefaultValue:nil] fontSize:[options.backButton.fontSize getWithDefaultValue:nil]];
}
}

Expand Down Expand Up @@ -111,21 +111,27 @@ - (void)setLargeTitleAttributes:(RNNLargeTitleOptions *)largeTitleOptions {
}
}

- (void)setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title showTitle:(BOOL)showTitle {
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
- (void)setBackButtonOptions:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title showTitle:(BOOL)showTitle fontFamily:(NSString *)fontFamily fontSize:(NSNumber *)fontSize {
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
NSArray* stackChildren = self.navigationController.viewControllers;
icon = color
? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
: icon;
[self setBackIndicatorImage:icon withColor:color];

UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;

if (showTitle) {
backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
}
backItem.tintColor = color;


if (fontFamily) {
CGFloat resolvedFontSize = fontSize ? fontSize.floatValue : 17.0;
[backItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:fontFamily size:resolvedFontSize], NSFontAttributeName, nil] forState:UIControlStateNormal];
[backItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:fontFamily size:resolvedFontSize], NSFontAttributeName, nil] forState:UIControlStateHighlighted];
}

lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/src/interfaces/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ export interface OptionsTopBarBackButton {
* Back button icon and text color
*/
color?: Color;
/**
* Set subtitle font size
*/
fontSize?: number;
/**
* Set subtitle font family
*/
fontFamily?: FontFamily;
}

export interface OptionsTopBarBackground {
Expand Down
5 changes: 5 additions & 0 deletions playground/ios/NavigationTests/RNNNavigationOptionsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ - (void)testAddsStyleFromDictionaryWithInit {
XCTAssertTrue(options.topBar.background.color);
}

- (void)testAddsFontToBackButtonOptions {
RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{@"topBar": @{@"backButton" : @{@"fontFamily" : @"HelveticaNeue"}}}];
XCTAssertTrue([options.topBar.backButton.fontFamily.get isEqual:@"HelveticaNeue"]);
}

- (void)testChangeRNNNavigationOptionsDynamically {
RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{@"topBar": @{@"background" : @{@"color" : @(0xff0000ff)}}}];
NSDictionary* dynamicOptionsDict = @{@"topBar": @{@"textColor" : @(0xffff00ff), @"title" : @{@"text": @"hello"}}};
Expand Down

0 comments on commit b438588

Please sign in to comment.