|
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)setBackgroundColor:(UIColor *)backgroundColor |
43 | | -{ |
44 | | - #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ |
45 | | - __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 |
46 | | - if (@available(iOS 13.0, *)) { |
47 | | - [super setBackgroundColor:backgroundColor]; |
48 | | - } |
49 | | - #endif |
50 | | -} |
51 | | - |
52 | | -- (void)setTextColor:(UIColor *)textColor |
53 | | -{ |
54 | | - #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ |
55 | | - __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 |
56 | | - if (@available(iOS 13.0, *)) { |
57 | | - [_attributes setObject: textColor forKey:NSForegroundColorAttributeName]; |
58 | | - [self setTitleTextAttributes:_attributes |
59 | | - forState:UIControlStateNormal]; |
60 | | - } |
61 | | - #endif |
62 | | -} |
63 | | - |
64 | | -- (void)setActiveTextColor:(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:UIControlStateSelected]; |
72 | | - } |
73 | | - #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 |
74 | 48 | } |
75 | 49 |
|
76 | | -- (void)setTintColor:(UIColor *)tintColor |
77 | | -{ |
| 50 | +- (void)setTintColor:(UIColor *)tintColor { |
78 | 51 | [super setTintColor:tintColor]; |
79 | | -#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ |
| 52 | +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ |
80 | 53 | __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 |
81 | 54 | if (@available(iOS 13.0, *)) { |
82 | 55 | [self setSelectedSegmentTintColor:tintColor]; |
83 | | - [_attributes setObject: tintColor forKey:NSForegroundColorAttributeName]; |
84 | | - [self setTitleTextAttributes:_attributes |
85 | | - 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]; |
86 | 65 | } |
87 | 66 | #endif |
88 | 67 | } |
89 | 68 |
|
90 | | -- (void)didChange |
91 | | -{ |
| 69 | +- (void)didChange { |
92 | 70 | _selectedIndex = self.selectedSegmentIndex; |
93 | 71 | if (_onChange) { |
94 | 72 | _onChange(@{ |
95 | | - @"value": [self titleForSegmentAtIndex:_selectedIndex], |
96 | | - @"selectedSegmentIndex": @(_selectedIndex) |
| 73 | + @"value" : [self titleForSegmentAtIndex:_selectedIndex], |
| 74 | + @"selectedSegmentIndex" : @(_selectedIndex) |
97 | 75 | }); |
98 | 76 | } |
99 | 77 | } |
100 | 78 |
|
101 | | -- (void)setAppearance:(NSString *)appearanceString |
102 | | -{ |
103 | | -#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) && \ |
104 | 81 | __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 |
105 | 82 | if (@available(iOS 13.0, *)) { |
106 | | - if ([appearanceString isEqual: @"dark"]) { |
107 | | - [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark]; |
108 | | - } else if ([appearanceString isEqual: @"light"]) { |
109 | | - [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight]; |
110 | | - } |
| 83 | + if ([appearanceString isEqual:@"dark"]) { |
| 84 | + [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark]; |
| 85 | + } else if ([appearanceString isEqual:@"light"]) { |
| 86 | + [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight]; |
| 87 | + } |
111 | 88 | } |
112 | 89 | #endif |
113 | 90 | } |
|
0 commit comments