Skip to content

Commit f071196

Browse files
yogevbdvshkl
authored andcommitted
Add Icon insets support for buttons (wix#4699)
* Add button.iconInsets * Define iconInsets in Options.ts * Apply color on button iconImage
1 parent 5c5fd40 commit f071196

File tree

10 files changed

+137
-9
lines changed

10 files changed

+137
-9
lines changed

lib/ios/RNNButtonOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "RNNOptions.h"
2+
#import "RNNInsetsOptions.h"
23

34
@interface RNNButtonOptions : RNNOptions
45

@@ -9,5 +10,6 @@
910
@property (nonatomic, strong) Color* disabledColor;
1011
@property (nonatomic, strong) Image* icon;
1112
@property (nonatomic, strong) Bool* enabled;
13+
@property (nonatomic, strong) RNNInsetsOptions* iconInsets;
1214

1315
@end

lib/ios/RNNButtonOptions.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
1111
self.color = [ColorParser parse:dict key:@"color"];
1212
self.disabledColor = [ColorParser parse:dict key:@"disabledColor"];
1313
self.icon = [ImageParser parse:dict key:@"icon"];
14+
self.iconInsets = [[RNNInsetsOptions alloc] initWithDict:dict];
1415
self.enabled = [BoolParser parse:dict key:@"enabled"];
1516

1617

lib/ios/RNNInsetsOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#import "RNNOptions.h"
2+
3+
@interface RNNInsetsOptions : RNNOptions
4+
5+
@property (nonatomic, strong) Double* top;
6+
@property (nonatomic, strong) Double* left;
7+
@property (nonatomic, strong) Double* right;
8+
@property (nonatomic, strong) Double* bottom;
9+
10+
@end

lib/ios/RNNInsetsOptions.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#import "RNNInsetsOptions.h"
2+
3+
@implementation RNNInsetsOptions
4+
5+
- (instancetype)initWithDict:(NSDictionary *)dict {
6+
self = [super init];
7+
8+
self.top = [DoubleParser parse:dict key:@"top"];
9+
self.left = [DoubleParser parse:dict key:@"left"];
10+
self.bottom = [DoubleParser parse:dict key:@"bottom"];
11+
self.right = [DoubleParser parse:dict key:@"right"];
12+
13+
return self;
14+
}
15+
16+
@end

lib/ios/RNNNavigationButtons.m

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#import "RCTHelpers.h"
55
#import "UIImage+tint.h"
66
#import "RNNRootViewController.h"
7+
#import "UIImage+insets.h"
78

89
@interface RNNNavigationButtons()
910

@@ -28,19 +29,19 @@ - (void)applyLeftButtons:(NSArray *)leftButtons rightButtons:(NSArray *)rightBut
2829
_defaultLeftButtonStyle = defaultLeftButtonStyle;
2930
_defaultRightButtonStyle = defaultRightButtonStyle;
3031
if (leftButtons) {
31-
[self setButtons:leftButtons side:@"left" animated:NO defaultStyle:_defaultLeftButtonStyle];
32+
[self setButtons:leftButtons side:@"left" animated:NO defaultStyle:_defaultLeftButtonStyle insets:[self leftButtonInsets:_defaultLeftButtonStyle.iconInsets]];
3233
}
3334

3435
if (rightButtons) {
35-
[self setButtons:rightButtons side:@"right" animated:NO defaultStyle:_defaultRightButtonStyle];
36+
[self setButtons:rightButtons side:@"right" animated:NO defaultStyle:_defaultRightButtonStyle insets:[self rightButtonInsets:_defaultRightButtonStyle.iconInsets]];
3637
}
3738
}
3839

39-
-(void)setButtons:(NSArray*)buttons side:(NSString*)side animated:(BOOL)animated defaultStyle:(RNNButtonOptions *)defaultStyle {
40+
-(void)setButtons:(NSArray*)buttons side:(NSString*)side animated:(BOOL)animated defaultStyle:(RNNButtonOptions *)defaultStyle insets:(UIEdgeInsets)insets {
4041
NSMutableArray *barButtonItems = [NSMutableArray new];
4142
NSArray* resolvedButtons = [self resolveButtons:buttons];
4243
for (NSDictionary *button in resolvedButtons) {
43-
RNNUIBarButtonItem* barButtonItem = [self buildButton:button defaultStyle:defaultStyle];
44+
RNNUIBarButtonItem* barButtonItem = [self buildButton:button defaultStyle:defaultStyle insets:insets];
4445
if(barButtonItem) {
4546
[barButtonItems addObject:barButtonItem];
4647
}
@@ -67,12 +68,15 @@ - (NSArray *)resolveButtons:(id)buttons {
6768
}
6869
}
6970

70-
-(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNButtonOptions *)defaultStyle {
71+
-(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNButtonOptions *)defaultStyle insets:(UIEdgeInsets)insets {
7172
NSString* buttonId = dictionary[@"id"];
7273
NSString* title = [self getValue:dictionary[@"text"] withDefault:[defaultStyle.text getWithDefaultValue:nil]];
7374
NSDictionary* component = dictionary[@"component"];
7475
NSString* systemItemName = dictionary[@"systemItem"];
7576

77+
UIColor* color = [self color:[RCTConvert UIColor:dictionary[@"color"]] defaultColor:[defaultStyle.color getWithDefaultValue:nil]];
78+
UIColor* disabledColor = [self color:[RCTConvert UIColor:dictionary[@"disabledColor"]] defaultColor:[defaultStyle.disabledColor getWithDefaultValue:nil]];
79+
7680
if (!buttonId) {
7781
@throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
7882
}
@@ -83,6 +87,14 @@ -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNBu
8387
iconImage = [RCTConvert UIImage:iconImage];
8488
}
8589

90+
if (iconImage) {
91+
iconImage = [iconImage imageWithInsets:insets];
92+
if (color) {
93+
iconImage = [iconImage withTintColor:color];
94+
}
95+
}
96+
97+
8698
RNNUIBarButtonItem *barButtonItem;
8799
if (component) {
88100
RNNComponentOptions* componentOptions = [RNNComponentOptions new];
@@ -116,8 +128,6 @@ -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNBu
116128
NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
117129
NSMutableDictionary* disabledTextAttributes = [[NSMutableDictionary alloc] init];
118130

119-
UIColor* color = [self color:[RCTConvert UIColor:dictionary[@"color"]] defaultColor:[defaultStyle.color getWithDefaultValue:nil]];
120-
UIColor* disabledColor = [self color:[RCTConvert UIColor:dictionary[@"disabledColor"]] defaultColor:[defaultStyle.disabledColor getWithDefaultValue:nil]];
121131
if (!enabledBool && disabledColor) {
122132
color = disabledColor;
123133
[disabledTextAttributes setObject:disabledColor forKey:NSForegroundColorAttributeName];
@@ -185,4 +195,18 @@ - (id)getValue:(id)value withDefault:(id)defaultValue {
185195
return value ? value : defaultValue;
186196
}
187197

198+
- (UIEdgeInsets)leftButtonInsets:(RNNInsetsOptions *)defaultInsets {
199+
return UIEdgeInsetsMake([defaultInsets.top getWithDefaultValue:0],
200+
[defaultInsets.left getWithDefaultValue:0],
201+
[defaultInsets.bottom getWithDefaultValue:0],
202+
[defaultInsets.right getWithDefaultValue:15]);
203+
}
204+
205+
- (UIEdgeInsets)rightButtonInsets:(RNNInsetsOptions *)defaultInsets {
206+
return UIEdgeInsetsMake([defaultInsets.top getWithDefaultValue:0],
207+
[defaultInsets.left getWithDefaultValue:15],
208+
[defaultInsets.bottom getWithDefaultValue:0],
209+
[defaultInsets.right getWithDefaultValue:0]);
210+
}
211+
188212
@end

lib/ios/RNNUIBarButtonItem.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
@implementation RNNUIBarButtonItem
77

88
-(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {
9-
self = [super initWithImage:iconImage style:UIBarButtonItemStylePlain target:nil action:nil];
9+
UIButton* button = [[UIButton alloc] init];
10+
[button addTarget:self action:@selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
11+
[button setImage:iconImage forState:UIControlStateNormal];
12+
self = [super initWithCustomView:button];
1013
self.buttonId = buttonId;
1114
return self;
1215
}
@@ -26,7 +29,7 @@ -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView
2629
self.buttonId = buttonId;
2730
return self;
2831
}
29-
32+
3033
-(instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
3134
UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
3235
self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
@@ -40,4 +43,10 @@ - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
4043
self.width = size.width;
4144
}
4245

46+
- (void)onButtonPressed {
47+
[self.target performSelector:self.action
48+
withObject:self
49+
afterDelay:0];
50+
}
51+
4352
@end

lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@
181181
505EDD4A214FDA800071C7DE /* RCTConvert+Modal.h in Headers */ = {isa = PBXBuildFile; fileRef = 505EDD48214FDA800071C7DE /* RCTConvert+Modal.h */; };
182182
5060DE73219DAD7E00D0C052 /* ReactNativeNavigation.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BA500731E2544B9001B9E1B /* ReactNativeNavigation.h */; };
183183
5060DE74219DAD8300D0C052 /* RNNBridgeManagerDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 502CB43920CBCA140019B2FE /* RNNBridgeManagerDelegate.h */; };
184+
506317AA220B547400B26FC3 /* UIImage+insets.h in Headers */ = {isa = PBXBuildFile; fileRef = 506317A8220B547400B26FC3 /* UIImage+insets.h */; };
185+
506317AB220B547400B26FC3 /* UIImage+insets.m in Sources */ = {isa = PBXBuildFile; fileRef = 506317A9220B547400B26FC3 /* UIImage+insets.m */; };
186+
506317AE220B550600B26FC3 /* RNNInsetsOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 506317AC220B550600B26FC3 /* RNNInsetsOptions.h */; };
187+
506317AF220B550600B26FC3 /* RNNInsetsOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 506317AD220B550600B26FC3 /* RNNInsetsOptions.m */; };
184188
5064495D20DC62B90026709C /* RNNSideMenuSideOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 5064495B20DC62B90026709C /* RNNSideMenuSideOptions.h */; };
185189
5064495E20DC62B90026709C /* RNNSideMenuSideOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5064495C20DC62B90026709C /* RNNSideMenuSideOptions.m */; };
186190
50644A2020E11A720026709C /* Constants.h in Headers */ = {isa = PBXBuildFile; fileRef = 50644A1E20E11A720026709C /* Constants.h */; };
@@ -508,6 +512,10 @@
508512
505EDD3B214FA8000071C7DE /* RNNViewControllerPresenter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNViewControllerPresenter.m; sourceTree = "<group>"; };
509513
505EDD47214FC4A60071C7DE /* RNNLayoutProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNLayoutProtocol.h; sourceTree = "<group>"; };
510514
505EDD48214FDA800071C7DE /* RCTConvert+Modal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+Modal.h"; sourceTree = "<group>"; };
515+
506317A8220B547400B26FC3 /* UIImage+insets.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+insets.h"; sourceTree = "<group>"; };
516+
506317A9220B547400B26FC3 /* UIImage+insets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+insets.m"; sourceTree = "<group>"; };
517+
506317AC220B550600B26FC3 /* RNNInsetsOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNInsetsOptions.h; sourceTree = "<group>"; };
518+
506317AD220B550600B26FC3 /* RNNInsetsOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNInsetsOptions.m; sourceTree = "<group>"; };
511519
5064495B20DC62B90026709C /* RNNSideMenuSideOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNSideMenuSideOptions.h; sourceTree = "<group>"; };
512520
5064495C20DC62B90026709C /* RNNSideMenuSideOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNSideMenuSideOptions.m; sourceTree = "<group>"; };
513521
50644A1E20E11A720026709C /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = "<group>"; };
@@ -697,6 +705,8 @@
697705
50644A1F20E11A720026709C /* Constants.m */,
698706
50706E6B20CE7CA5003345C3 /* UIImage+tint.h */,
699707
50706E6C20CE7CA5003345C3 /* UIImage+tint.m */,
708+
506317A8220B547400B26FC3 /* UIImage+insets.h */,
709+
506317A9220B547400B26FC3 /* UIImage+insets.m */,
700710
5038A372216CDDB6009280BC /* UIViewController+SideMenuController.h */,
701711
5038A373216CDDB6009280BC /* UIViewController+SideMenuController.m */,
702712
5038A3BF216E1E66009280BC /* RNNFontAttributesCreator.h */,
@@ -931,6 +941,8 @@
931941
E33AC20720B5C4F90090DB8A /* RNNSplitViewOptions.m */,
932942
E3458D3C20BD9CA10023149B /* RNNPreviewOptions.h */,
933943
E3458D3D20BD9CE40023149B /* RNNPreviewOptions.m */,
944+
506317AC220B550600B26FC3 /* RNNInsetsOptions.h */,
945+
506317AD220B550600B26FC3 /* RNNInsetsOptions.m */,
934946
);
935947
name = Options;
936948
sourceTree = "<group>";
@@ -1250,6 +1262,7 @@
12501262
50D031342005149000386B3D /* RNNOverlayManager.h in Headers */,
12511263
7B1126A71E2D2B6C00F9B03B /* RNNEventEmitter.h in Headers */,
12521264
E8A430111F9CB87B00B61A20 /* RNNAnimatedView.h in Headers */,
1265+
506317AA220B547400B26FC3 /* UIImage+insets.h in Headers */,
12531266
5038A3C6216E2D93009280BC /* Number.h in Headers */,
12541267
50887C1520ECC5C200D06111 /* RNNButtonOptions.h in Headers */,
12551268
5049593E216F5D73006D2B81 /* BoolParser.h in Headers */,
@@ -1299,6 +1312,7 @@
12991312
507F43C91FF4F9CC00D9425B /* RNNTopTabOptions.h in Headers */,
13001313
E8A5CD521F464F0400E89D0D /* RNNAnimator.h in Headers */,
13011314
50451D0D2042F70900695F00 /* RNNTransition.h in Headers */,
1315+
506317AE220B550600B26FC3 /* RNNInsetsOptions.h in Headers */,
13021316
507F43F81FF525B500D9425B /* RNNSegmentedControl.h in Headers */,
13031317
50175CD1207A2AA1004FE91B /* RNNComponentOptions.h in Headers */,
13041318
5038A3B1216DF41B009280BC /* UIViewController+RNNOptions.h in Headers */,
@@ -1481,6 +1495,7 @@
14811495
506A2B1520973DFD00F43A95 /* RNNErrorHandler.m in Sources */,
14821496
50395588217480C900B0A663 /* IntNumber.m in Sources */,
14831497
7BBFE5441E25330E002A6182 /* RNNBridgeModule.m in Sources */,
1498+
506317AF220B550600B26FC3 /* RNNInsetsOptions.m in Sources */,
14841499
261F0E651E6EC94900989DE2 /* RNNModalManager.m in Sources */,
14851500
50395590217482FE00B0A663 /* NullIntNumber.m in Sources */,
14861501
E8A5CD631F49114F00E89D0D /* RNNElement.m in Sources */,
@@ -1519,6 +1534,7 @@
15191534
E33AC20020B5BA0B0090DB8A /* RNNSplitViewController.m in Sources */,
15201535
263905CF1E4C6F440023D7D3 /* TheSidebarController.m in Sources */,
15211536
5038A3C2216E1E66009280BC /* RNNFontAttributesCreator.m in Sources */,
1537+
506317AB220B547400B26FC3 /* UIImage+insets.m in Sources */,
15221538
E8A430121F9CB87B00B61A20 /* RNNAnimatedView.m in Sources */,
15231539
507F43F51FF4FCFE00D9425B /* HMSegmentedControl.m in Sources */,
15241540
5012242321736883000F5F98 /* NullColor.m in Sources */,

lib/ios/UIImage+insets.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import <UIKit/UIKit.h>
2+
3+
@interface UIImage (insets)
4+
5+
- (UIImage *)imageWithInsets:(UIEdgeInsets)insets;
6+
7+
@end

lib/ios/UIImage+insets.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#import "UIImage+insets.h"
2+
3+
@implementation UIImage (insets)
4+
5+
- (UIImage *)imageWithInsets:(UIEdgeInsets)insets {
6+
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.size.width + insets.left + insets.right,
7+
self.size.height + insets.top + insets.bottom), false, self.scale);
8+
CGContextRef context = UIGraphicsGetCurrentContext();
9+
UIGraphicsPushContext(context);
10+
11+
CGPoint origin = CGPointMake(insets.left, insets.top);
12+
[self drawAtPoint:origin];
13+
14+
UIGraphicsPopContext();
15+
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
16+
UIGraphicsEndImageContext();
17+
return newImage;
18+
}
19+
20+
@end

lib/src/interfaces/Options.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ export interface OptionsTopBarButton {
247247
* Set the button icon
248248
*/
249249
icon?: ImageRequireSource;
250+
/**
251+
* Set the button icon insets
252+
*/
253+
iconInsets?: IconInsets;
250254
/**
251255
* Set the button as a custom component
252256
*/
@@ -692,6 +696,25 @@ export interface OptionsAnimationProperties {
692696
rotation?: OptionsAnimationPropertyConfig;
693697
}
694698

699+
export interface IconInsets {
700+
/**
701+
* Configure top inset
702+
*/
703+
top?: number;
704+
/**
705+
* Configure left inset
706+
*/
707+
left?: number;
708+
/**
709+
* Configure bottom inset
710+
*/
711+
bottom?: number;
712+
/**
713+
* Configure right inset
714+
*/
715+
right?: number;
716+
}
717+
695718
export interface OptionsAnimationPropertiesId extends OptionsAnimationProperties {
696719
/**
697720
* ID of the Top Bar we want to animate

0 commit comments

Comments
 (0)