|
13 | 13 |
|
14 | 14 | @implementation RNCSegmentedControl
|
15 | 15 |
|
16 |
| -- (instancetype)initWithFrame:(CGRect)frame |
17 |
| -{ |
| 16 | +- (instancetype)initWithFrame:(CGRect)frame { |
18 | 17 | if ((self = [super initWithFrame:frame])) {
|
19 | 18 | _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]; |
23 | 22 | }
|
24 | 23 | return self;
|
25 | 24 | }
|
26 | 25 |
|
27 |
| -- (void)setValues:(NSArray<NSString *> *)values |
28 |
| -{ |
| 26 | +- (void)setValues:(NSArray<NSString *> *)values { |
29 | 27 | [self removeAllSegments];
|
30 | 28 | 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]; |
32 | 32 | }
|
33 | 33 | super.selectedSegmentIndex = _selectedIndex;
|
34 | 34 | }
|
35 | 35 |
|
36 |
| -- (void)setSelectedIndex:(NSInteger)selectedIndex |
37 |
| -{ |
| 36 | +- (void)setSelectedIndex:(NSInteger)selectedIndex { |
38 | 37 | _selectedIndex = selectedIndex;
|
39 | 38 | super.selectedSegmentIndex = selectedIndex;
|
40 | 39 | }
|
41 | 40 |
|
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 |
86 | 48 | }
|
87 | 49 |
|
88 |
| -- (void)setTintColor:(UIColor *)tintColor |
89 |
| -{ |
| 50 | +- (void)setTintColor:(UIColor *)tintColor { |
90 | 51 | [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) && \ |
92 | 53 | __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
|
93 | 54 | if (@available(iOS 13.0, *)) {
|
94 | 55 | [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]; |
98 | 65 | }
|
99 | 66 | #endif
|
100 | 67 | }
|
101 | 68 |
|
102 |
| -- (void)didChange |
103 |
| -{ |
| 69 | +- (void)didChange { |
104 | 70 | _selectedIndex = self.selectedSegmentIndex;
|
105 | 71 | if (_onChange) {
|
106 | 72 | _onChange(@{
|
107 |
| - @"value": [self titleForSegmentAtIndex:_selectedIndex], |
108 |
| - @"selectedSegmentIndex": @(_selectedIndex) |
| 73 | + @"value" : [self titleForSegmentAtIndex:_selectedIndex], |
| 74 | + @"selectedSegmentIndex" : @(_selectedIndex) |
109 | 75 | });
|
110 | 76 | }
|
111 | 77 | }
|
112 | 78 |
|
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) && \ |
116 | 81 | __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
|
117 | 82 | 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 | + } |
123 | 88 | }
|
124 | 89 | #endif
|
125 | 90 | }
|
|
0 commit comments