Skip to content

Commit c33978d

Browse files
Naturalclararjndr
andauthored
2.0.0 (#90)
Co-authored-by: Akash Rajendra <[email protected]>
1 parent 984aeb6 commit c33978d

13 files changed

+1034
-864
lines changed

example/App.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,19 @@ export default class App extends React.Component<{}, $FlowFixMeState> {
2323
<Text style={styles.text}>Segmented controls can have values</Text>
2424
<SegmentedControl values={['One', 'Two']} />
2525
</View>
26-
2726
<View style={styles.segmentSection}>
2827
<SegmentedControl values={['One', 'Two', 'Three', 'Four', 'Five']} />
2928
</View>
30-
3129
<View style={styles.segmentSection}>
3230
<Text style={styles.text}>
3331
Segmented controls can have pre-selected values
3432
</Text>
3533
<SegmentedControl values={['One', 'Two']} selectedIndex={0} />
3634
</View>
37-
3835
<View style={styles.segmentSection}>
3936
<Text style={styles.text}>Segmented controls can be momentary</Text>
4037
<SegmentedControl values={['One', 'Two']} momentary={true} />
4138
</View>
42-
4339
<View style={styles.segmentSection}>
4440
<Text style={styles.text}>Segmented controls can be disabled</Text>
4541
<SegmentedControl
@@ -48,28 +44,25 @@ export default class App extends React.Component<{}, $FlowFixMeState> {
4844
selectedIndex={1}
4945
/>
5046
</View>
51-
5247
<View style={styles.segmentContainer}>
5348
<Text style={styles.text}>Custom colors can be provided</Text>
5449
<SegmentedControl
5550
tintColor="#ff0000"
5651
values={['One', 'Two', 'Three', 'Four']}
5752
selectedIndex={0}
5853
backgroundColor="#0000ff"
59-
activeTextColor="white"
6054
/>
6155
</View>
6256
<View style={styles.segmentContainer}>
6357
<SegmentedControl
6458
tintColor="#00ff00"
6559
values={['One', 'Two', 'Three']}
6660
selectedIndex={1}
67-
activeTextColor="black"
6861
/>
6962
</View>
7063
<View style={styles.segmentSection}>
7164
<SegmentedControl
72-
textColor="#ff00ff"
65+
fontStyle={{color: '#ff00ff'}}
7366
values={['One', 'Two']}
7467
selectedIndex={1}
7568
/>
@@ -82,16 +75,18 @@ export default class App extends React.Component<{}, $FlowFixMeState> {
8275
<SegmentedControl
8376
values={['One', 'Two']}
8477
style={{height: 80}}
85-
fontSize={32}
78+
fontStyle={styles.fontStyle}
8679
/>
8780
</View>
8881
<SegmentedControl
8982
values={['One', 'Two']}
83+
tintColor="red"
9084
style={{height: 80}}
91-
fontSize={32}
92-
textColor="blue"
93-
tintColor="green"
94-
activeTextColor="red"
85+
fontStyle={{
86+
fontFamily: 'Optima',
87+
fontSize: 32,
88+
}}
89+
activeFontStyle={styles.activeFontStyle}
9590
/>
9691
</View>
9792
<View>
@@ -141,4 +136,13 @@ const styles = StyleSheet.create({
141136
container: {
142137
paddingTop: 80,
143138
},
139+
fontStyle: {
140+
fontSize: 32,
141+
fontFamily: 'serif',
142+
},
143+
activeFontStyle: {
144+
fontSize: 40,
145+
fontFamily: 'Optima-Bold',
146+
color: 'blue',
147+
},
144148
});

example/ios/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ target 'example' do
7676
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
7777
pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
7878
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
79+
7980

8081
target 'exampleTests' do
8182
inherit! :complete

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,6 @@ SPEC CHECKSUMS:
457457
Yoga: 3ebccbdd559724312790e7742142d062476b698e
458458
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
459459

460-
PODFILE CHECKSUM: 56c2537f71f3f02200d6918c542a8e89a0b422fa
460+
PODFILE CHECKSUM: 79ca0d949f09a7c2ff51fed03be24f2aa963d8da
461461

462-
COCOAPODS: 1.9.1
462+
COCOAPODS: 1.8.4

index.d.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ export interface NativeSegmentedControlIOSChangeEvent extends TargetedEvent {
1515
selectedSegmentIndex: number;
1616
}
1717

18+
export type FontStyle = {
19+
/**
20+
* Font Color of Segmented Control when Active
21+
*/
22+
color?: string;
23+
/**
24+
* Font Size of Segmented Control when Active
25+
*/
26+
fontSize?: number;
27+
/**
28+
* Font Family of the Segmented Control when Active
29+
*/
30+
fontFamily?: string;
31+
};
32+
1833
export interface SegmentedControlProps extends ViewProps {
1934
/**
2035
* If false the user won't be able to interact with the control. Default value is true.
@@ -51,19 +66,7 @@ export interface SegmentedControlProps extends ViewProps {
5166
tintColor?: string;
5267

5368
/**
54-
* (iOS 13 only)
55-
* Text color of the control.
56-
*/
57-
textColor?: string;
58-
59-
/**
60-
* (iOS 13 only)
61-
* Text color of the active control.
62-
*/
63-
activeTextColor?: string;
64-
65-
/**
66-
* (iOS 13 only)
69+
* (iOS 13+ only)
6770
* Background color of the control.
6871
*/
6972
backgroundColor?: string;
@@ -74,14 +77,18 @@ export interface SegmentedControlProps extends ViewProps {
7477
values?: string[];
7578

7679
/**
77-
* (iOS 13 only)
80+
* (iOS 13+ only)
7881
* Overrides the control's appearance irrespective of the OS theme
7982
*/
8083
appearance?: 'dark' | 'light';
8184
/**
82-
* Font Size of Segmented Control
85+
* Font style properties of the Segmented Control
8386
*/
84-
fontSize?: number;
87+
fontStyle?: FontStyle;
88+
/**
89+
* Active Font style properties of the Segmented Control
90+
*/
91+
activeFontStyle?: FontStyle;
8592
}
8693

8794
/**

ios/RNCSegmentedControl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
*/
77

88
#import <UIKit/UIKit.h>
9-
109
#import <React/RCTComponent.h>
1110

1211
@interface RNCSegmentedControl : UISegmentedControl
13-
@property (nonatomic, copy) NSMutableDictionary *attributes;
14-
@property (nonatomic, assign) NSInteger selectedIndex;
15-
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
12+
@property(nonatomic, assign) NSInteger selectedIndex;
13+
@property(nonatomic, copy) RCTBubblingEventBlock onChange;
1614

1715
@end

ios/RNCSegmentedControl.m

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,113 +13,78 @@
1313

1414
@implementation RNCSegmentedControl
1515

16-
- (instancetype)initWithFrame:(CGRect)frame
17-
{
16+
- (instancetype)initWithFrame:(CGRect)frame {
1817
if ((self = [super initWithFrame:frame])) {
1918
_selectedIndex = self.selectedSegmentIndex;
20-
[self addTarget:self action:@selector(didChange)
21-
forControlEvents:UIControlEventValueChanged];
22-
_attributes = [[NSMutableDictionary alloc] init];
19+
[self addTarget:self
20+
action:@selector(didChange)
21+
forControlEvents:UIControlEventValueChanged];
2322
}
2423
return self;
2524
}
2625

27-
- (void)setValues:(NSArray<NSString *> *)values
28-
{
26+
- (void)setValues:(NSArray<NSString *> *)values {
2927
[self removeAllSegments];
3028
for (NSString *value in values) {
31-
[self insertSegmentWithTitle:value atIndex:self.numberOfSegments animated:NO];
29+
[self insertSegmentWithTitle:value
30+
atIndex:self.numberOfSegments
31+
animated:NO];
3232
}
3333
super.selectedSegmentIndex = _selectedIndex;
3434
}
3535

36-
- (void)setSelectedIndex:(NSInteger)selectedIndex
37-
{
36+
- (void)setSelectedIndex:(NSInteger)selectedIndex {
3837
_selectedIndex = selectedIndex;
3938
super.selectedSegmentIndex = selectedIndex;
4039
}
4140

42-
- (void)setFontSize:(NSInteger)fontSize
43-
{
44-
UIFont *font = [UIFont systemFontOfSize: fontSize];
45-
[_attributes setObject: font forKey:NSFontAttributeName];
46-
[self setTitleTextAttributes:_attributes
47-
forState:UIControlStateNormal];
48-
UIFont *fontBold = [UIFont boldSystemFontOfSize: fontSize];
49-
[_attributes setObject: fontBold forKey:NSFontAttributeName];
50-
[self setTitleTextAttributes:_attributes
51-
forState:UIControlStateSelected];
52-
}
53-
54-
- (void)setBackgroundColor:(UIColor *)backgroundColor
55-
{
56-
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
57-
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
58-
if (@available(iOS 13.0, *)) {
59-
[super setBackgroundColor:backgroundColor];
60-
}
61-
#endif
62-
}
63-
64-
- (void)setTextColor:(UIColor *)textColor
65-
{
66-
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
67-
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
68-
if (@available(iOS 13.0, *)) {
69-
[_attributes setObject: textColor forKey:NSForegroundColorAttributeName];
70-
[self setTitleTextAttributes:_attributes
71-
forState:UIControlStateNormal];
72-
}
73-
#endif
74-
}
75-
76-
- (void)setActiveTextColor:(UIColor *)textColor
77-
{
78-
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
79-
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
80-
if (@available(iOS 13.0, *)) {
81-
[_attributes setObject: textColor forKey:NSForegroundColorAttributeName];
82-
[self setTitleTextAttributes:_attributes
83-
forState:UIControlStateSelected];
84-
}
85-
#endif
41+
- (void)setBackgroundColor:(UIColor *)backgroundColor {
42+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
43+
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
44+
if (@available(iOS 13.0, *)) {
45+
[super setBackgroundColor:backgroundColor];
46+
}
47+
#endif
8648
}
8749

88-
- (void)setTintColor:(UIColor *)tintColor
89-
{
50+
- (void)setTintColor:(UIColor *)tintColor {
9051
[super setTintColor:tintColor];
91-
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
52+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
9253
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
9354
if (@available(iOS 13.0, *)) {
9455
[self setSelectedSegmentTintColor:tintColor];
95-
[_attributes setObject: tintColor forKey:NSForegroundColorAttributeName];
96-
[self setTitleTextAttributes:_attributes
97-
forState:UIControlStateNormal];
56+
NSDictionary *attributes = [NSDictionary
57+
dictionaryWithObjectsAndKeys:tintColor, NSForegroundColorAttributeName,
58+
nil];
59+
NSDictionary *activeAttributes = [NSDictionary
60+
dictionaryWithObjectsAndKeys:UIColor.labelColor,
61+
NSForegroundColorAttributeName, nil];
62+
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];
63+
[self setTitleTextAttributes:activeAttributes
64+
forState:UIControlStateSelected];
9865
}
9966
#endif
10067
}
10168

102-
- (void)didChange
103-
{
69+
- (void)didChange {
10470
_selectedIndex = self.selectedSegmentIndex;
10571
if (_onChange) {
10672
_onChange(@{
107-
@"value": [self titleForSegmentAtIndex:_selectedIndex],
108-
@"selectedSegmentIndex": @(_selectedIndex)
73+
@"value" : [self titleForSegmentAtIndex:_selectedIndex],
74+
@"selectedSegmentIndex" : @(_selectedIndex)
10975
});
11076
}
11177
}
11278

113-
- (void)setAppearance:(NSString *)appearanceString
114-
{
115-
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
79+
- (void)setAppearance:(NSString *)appearanceString {
80+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
11681
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
11782
if (@available(iOS 13.0, *)) {
118-
if ([appearanceString isEqual: @"dark"]) {
119-
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
120-
} else if ([appearanceString isEqual: @"light"]) {
121-
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
122-
}
83+
if ([appearanceString isEqual:@"dark"]) {
84+
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
85+
} else if ([appearanceString isEqual:@"light"]) {
86+
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
87+
}
12388
}
12489
#endif
12590
}

0 commit comments

Comments
 (0)