Skip to content

Commit efebede

Browse files
authored
feat: allow changing fontSize (#72)
1 parent 0247a5b commit efebede

File tree

11 files changed

+269
-7598
lines changed

11 files changed

+269
-7598
lines changed

example/App.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ export default class App extends React.Component<{}, $FlowFixMeState> {
7474
selectedIndex={1}
7575
/>
7676
</View>
77-
77+
<View style={styles.segmentContainer}>
78+
<Text style={styles.text}>
79+
Segmented controls can have defined fontSize
80+
</Text>
81+
<SegmentedControl
82+
values={['One', 'Two']}
83+
style={{height: 80}}
84+
fontSize={32}
85+
/>
86+
</View>
7887
<View>
7988
<Text style={styles.text}>Custom colors can be provided</Text>
8089
<View style={styles.segmentContainer}>

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ PODS:
182182
- React-cxxreact (= 0.61.5)
183183
- React-jsi (= 0.61.5)
184184
- React-jsinspector (0.61.5)
185-
- react-native-segmented-control (1.3.0):
185+
- react-native-segmented-control (1.4.0):
186186
- React
187187
- React-RCTActionSheet (0.61.5):
188188
- React-Core/RCTActionSheetHeaders (= 0.61.5)
@@ -326,7 +326,7 @@ SPEC CHECKSUMS:
326326
React-jsi: cb2cd74d7ccf4cffb071a46833613edc79cdf8f7
327327
React-jsiexecutor: d5525f9ed5f782fdbacb64b9b01a43a9323d2386
328328
React-jsinspector: fa0ecc501688c3c4c34f28834a76302233e29dc0
329-
react-native-segmented-control: 64a281fade11306631dccd3bbeac6e55b5ab362f
329+
react-native-segmented-control: 6ad7533758f4567e1e97136a67986f4167fd106a
330330
React-RCTActionSheet: 600b4d10e3aea0913b5a92256d2719c0cdd26d76
331331
React-RCTAnimation: 791a87558389c80908ed06cc5dfc5e7920dfa360
332332
React-RCTBlob: d89293cc0236d9cb0933d85e430b0bbe81ad1d72
@@ -341,4 +341,4 @@ SPEC CHECKSUMS:
341341

342342
PODFILE CHECKSUM: 79310af6b976c356911a8a833e9b99c3399fdda3
343343

344-
COCOAPODS: 1.8.4
344+
COCOAPODS: 1.9.1

example/ios/example.xcodeproj/project.pbxproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
6BE7AEF78095A785F4869399 /* Pods-exampleTests.debug.xcconfig */,
162162
50618065BE0BA2D84E81F280 /* Pods-exampleTests.release.xcconfig */,
163163
);
164-
name = Pods;
165164
path = Pods;
166165
sourceTree = "<group>";
167166
};
@@ -305,7 +304,7 @@
305304
};
306305
buildConfigurationList = 83CBB9FA1A601CBA00E9B192 /* Build configuration list for PBXProject "example" */;
307306
compatibilityVersion = "Xcode 3.2";
308-
developmentRegion = English;
307+
developmentRegion = en;
309308
hasScannedForEncodings = 0;
310309
knownRegions = (
311310
en,
@@ -637,6 +636,7 @@
637636
);
638637
PRODUCT_BUNDLE_IDENTIFIER = "org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)";
639638
PRODUCT_NAME = example;
639+
TARGETED_DEVICE_FAMILY = "1,2";
640640
VERSIONING_SYSTEM = "apple-generic";
641641
};
642642
name = Debug;
@@ -656,6 +656,7 @@
656656
);
657657
PRODUCT_BUNDLE_IDENTIFIER = "org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)";
658658
PRODUCT_NAME = example;
659+
TARGETED_DEVICE_FAMILY = "1,2";
659660
VERSIONING_SYSTEM = "apple-generic";
660661
};
661662
name = Release;

example/yarn.lock

Lines changed: 216 additions & 7585 deletions
Large diffs are not rendered by default.

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export interface SegmentedControlProps extends ViewProps {
7878
* Overrides the control's appearance irrespective of the OS theme
7979
*/
8080
appearance?: 'dark' | 'light';
81+
/**
82+
* Font Size of Segmented Control
83+
*/
84+
fontSize?: number;
8185
}
8286

8387
/**

ios/RNCSegmentedControl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import <React/RCTComponent.h>
1111

1212
@interface RNCSegmentedControl : UISegmentedControl
13-
13+
@property (nonatomic, copy) NSMutableDictionary *attributes;
1414
@property (nonatomic, assign) NSInteger selectedIndex;
1515
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
1616

ios/RNCSegmentedControl.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ - (instancetype)initWithFrame:(CGRect)frame
1919
_selectedIndex = self.selectedSegmentIndex;
2020
[self addTarget:self action:@selector(didChange)
2121
forControlEvents:UIControlEventValueChanged];
22+
_attributes = [[NSMutableDictionary alloc] init];
2223
}
2324
return self;
2425
}
@@ -38,6 +39,14 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex
3839
super.selectedSegmentIndex = selectedIndex;
3940
}
4041

42+
- (void)setFontSize:(NSInteger)fontSize
43+
{
44+
UIFont *font = [UIFont boldSystemFontOfSize: fontSize];
45+
[_attributes setObject: font forKey:NSFontAttributeName];
46+
[self setTitleTextAttributes:_attributes
47+
forState:UIControlStateNormal];
48+
}
49+
4150
- (void)setBackgroundColor:(UIColor *)backgroundColor
4251
{
4352
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
@@ -53,8 +62,9 @@ - (void)setTextColor:(UIColor *)textColor
5362
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
5463
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
5564
if (@available(iOS 13.0, *)) {
56-
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}
57-
forState:UIControlStateNormal];
65+
[_attributes setObject: textColor forKey:NSForegroundColorAttributeName];
66+
[self setTitleTextAttributes:_attributes
67+
forState:UIControlStateNormal];
5868
}
5969
#endif
6070
}

ios/RNCSegmentedControlManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ - (UIView *)view
2525
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
2626
RCT_EXPORT_VIEW_PROPERTY(backgroundColor, UIColor)
2727
RCT_EXPORT_VIEW_PROPERTY(textColor, UIColor)
28+
RCT_EXPORT_VIEW_PROPERTY(fontSize, NSInteger)
2829
RCT_EXPORT_VIEW_PROPERTY(activeTextColor, UIColor)
2930
RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL)
3031
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)

js/SegmentedControl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const SegmentedControl = ({
2525
tintColor,
2626
textColor,
2727
backgroundColor,
28+
fontSize,
2829
}: SegmentedControlProps) => {
2930
const handleChange = (index: number) => {
3031
// mocks iOS's nativeEvent
@@ -55,6 +56,7 @@ const SegmentedControl = ({
5556
value={value}
5657
tintColor={tintColor}
5758
textColor={textColor}
59+
fontSize={fontSize}
5860
activeTextColor={activeTextColor}
5961
onSelect={() => {
6062
handleChange(index);

js/SegmentedControlTab.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Props = $ReadOnly<{|
1515
onSelect: () => void,
1616
selected: boolean,
1717
enabled: boolean,
18+
fontSize?: ?number,
1819
|}>;
1920

2021
export const SegmentedControlTab = ({
@@ -25,6 +26,7 @@ export const SegmentedControlTab = ({
2526
tintColor,
2627
textColor,
2728
activeTextColor,
29+
fontSize,
2830
}: Props) => {
2931
const getColor = () => {
3032
if (selected && activeTextColor) {
@@ -56,7 +58,14 @@ export const SegmentedControlTab = ({
5658
styles.default,
5759
selected && {backgroundColor: getBackgroundColor()},
5860
]}>
59-
<Text style={[{color}, selected && styles.activeText]}>{value}</Text>
61+
<Text
62+
style={[
63+
{color},
64+
!(fontSize == null) && {fontSize},
65+
selected && styles.activeText,
66+
]}>
67+
{value}
68+
</Text>
6069
</View>
6170
</TouchableOpacity>
6271
);

0 commit comments

Comments
 (0)