Skip to content

Commit fefc0a5

Browse files
authored
feat: Adds fontFamily support on iOS, Closes #85 (#87)
1 parent 984aeb6 commit fefc0a5

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,22 @@ export interface SegmentedControlProps extends ViewProps {
7979
*/
8080
appearance?: 'dark' | 'light';
8181
/**
82+
* Font style properties of the Segmented Control
83+
*/
84+
fontStyle?: {
85+
/**
8286
* Font Size of Segmented Control
8387
*/
84-
fontSize?: number;
88+
fontSize?: number;
89+
/**
90+
* Font Family of the Segmented Control when idle/normal
91+
*/
92+
fontFamilyIdle?: string;
93+
/**
94+
* Font Family of the Segmented Control when selected
95+
*/
96+
fontFamilySelected?: string;
97+
}
8598
}
8699

87100
/**

ios/RNCSegmentedControl.m

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex
3939
super.selectedSegmentIndex = selectedIndex;
4040
}
4141

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-
5442
- (void)setBackgroundColor:(UIColor *)backgroundColor
5543
{
5644
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \

ios/RNCSegmentedControlManager.m

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,33 @@ - (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)
2928
RCT_EXPORT_VIEW_PROPERTY(activeTextColor, UIColor)
3029
RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL)
3130
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
3231
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
3332
RCT_EXPORT_VIEW_PROPERTY(appearance, NSString)
3433

34+
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl)
35+
{
36+
if (json) {
37+
NSInteger fontSize = json[@"fontSize"] ? [RCTConvert NSInteger:json[@"fontSize"]] : 13.0;
38+
39+
UIFont *idleFont = [UIFont systemFontOfSize: fontSize];
40+
UIFont *selectedFont = [UIFont boldSystemFontOfSize: fontSize];
41+
42+
if (json[@"fontFamilyIdle"] && json[@"fontFamilySelected"]) {
43+
idleFont = [UIFont fontWithName:json[@"fontFamilyIdle"] size:fontSize];
44+
selectedFont = [UIFont fontWithName:json[@"fontFamilySelected"] size:fontSize];
45+
} else if (json[@"fontFamilyIdle"]) {
46+
idleFont = [UIFont fontWithName:json[@"fontFamilyIdle"] size:fontSize];
47+
} else if (json[@"fontFamilySelected"]) {
48+
selectedFont = [UIFont fontWithName:json[@"fontFamilySelected"] size:fontSize];
49+
}
50+
51+
[view setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:idleFont, NSFontAttributeName, nil] forState:UIControlStateNormal];
52+
53+
[view setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:selectedFont, NSFontAttributeName, nil] forState:UIControlStateSelected];
54+
}
55+
}
56+
3557
@end

0 commit comments

Comments
 (0)