-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
RNCSegmentedControl.m
127 lines (113 loc) · 3.84 KB
/
RNCSegmentedControl.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNCSegmentedControl.h"
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/UIView+React.h>
@implementation RNCSegmentedControl
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_selectedIndex = self.selectedSegmentIndex;
[self addTarget:self action:@selector(didChange)
forControlEvents:UIControlEventValueChanged];
_attributes = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setValues:(NSArray<NSString *> *)values
{
[self removeAllSegments];
for (NSString *value in values) {
[self insertSegmentWithTitle:value atIndex:self.numberOfSegments animated:NO];
}
super.selectedSegmentIndex = _selectedIndex;
}
- (void)setSelectedIndex:(NSInteger)selectedIndex
{
_selectedIndex = selectedIndex;
super.selectedSegmentIndex = selectedIndex;
}
- (void)setFontSize:(NSInteger)fontSize
{
UIFont *font = [UIFont systemFontOfSize: fontSize];
[_attributes setObject: font forKey:NSFontAttributeName];
[self setTitleTextAttributes:_attributes
forState:UIControlStateNormal];
UIFont *fontBold = [UIFont boldSystemFontOfSize: fontSize];
[_attributes setObject: fontBold forKey:NSFontAttributeName];
[self setTitleTextAttributes:_attributes
forState:UIControlStateSelected];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[super setBackgroundColor:backgroundColor];
}
#endif
}
- (void)setTextColor:(UIColor *)textColor
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[_attributes setObject: textColor forKey:NSForegroundColorAttributeName];
[self setTitleTextAttributes:_attributes
forState:UIControlStateNormal];
}
#endif
}
- (void)setActiveTextColor:(UIColor *)textColor
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[_attributes setObject: textColor forKey:NSForegroundColorAttributeName];
[self setTitleTextAttributes:_attributes
forState:UIControlStateSelected];
}
#endif
}
- (void)setTintColor:(UIColor *)tintColor
{
[super setTintColor:tintColor];
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[self setSelectedSegmentTintColor:tintColor];
[_attributes setObject: tintColor forKey:NSForegroundColorAttributeName];
[self setTitleTextAttributes:_attributes
forState:UIControlStateNormal];
}
#endif
}
- (void)didChange
{
_selectedIndex = self.selectedSegmentIndex;
if (_onChange) {
_onChange(@{
@"value": [self titleForSegmentAtIndex:_selectedIndex],
@"selectedSegmentIndex": @(_selectedIndex)
});
}
}
- (void)setAppearance:(NSString *)appearanceString
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
if ([appearanceString isEqual: @"dark"]) {
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
} else if ([appearanceString isEqual: @"light"]) {
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
}
}
#endif
}
@end