Skip to content

Commit 18f1c69

Browse files
tom-ungrabbou
authored andcommittedApr 21, 2020
Allow iOS PlatformColor strings to be ObjC or Swift UIColor selectors (#28703)
Summary: Per discussion in react-native-community/releases#186 the iOS `PlatformColor()` function is documented to use the semantic color names provided by the system. The referenced HIG documentation itself links to the `UIColor` documentation for semantic colors names. However, these names differ depending on if you are viewing the new Swift API docs or the Objective C docs. The current Objective C implementation in react-native assumes Objective C UIColor selector names that are suffixed 'Color'. But in Swift, Apple provides a Swift Extension on UIColor that makes aliases without the the 'Color' suffix and then makes the original selectors invalid presumably via `NS_UNAVAILABLE_SWIFT`. Since both selector names are valid depending on if you are using Objective C or Swift, let's make both forms be legal for `PlatformColor()`. In `RCTConvert.m` there is a dictionary of legal selector names. The code already supports the ability to have names be aliases of other selectors via a RCTSelector metadata key. The change adds code to the initialization of the map: it iterates over the keys in the map, which are all ObjC style UIColor selectors, and creates aliases by duplicating the entries, creating key names by stripping off the ObjC "Color" suffix, adds the RCTSelector key referring to the original and then appends these new Swift aliases to the map. ## Changelog [iOS] [Changed] - Allow iOS PlatformColor strings to be ObjC or Swift UIColor selectors Pull Request resolved: #28703 Test Plan: The PlatformColorExample.js is updated to use the new, shorter Swift selector names. There are still other examples in the same file and in unit tests that exercise the ObjC selector names. <img width="492" alt="PlatformColor" src="https://user-images.githubusercontent.com/30053638/79809089-89ab7d00-8324-11ea-8a9d-120b92edeedf.png"> Reviewed By: shergin Differential Revision: D21147404 Pulled By: TheSavior fbshipit-source-id: 0273ec855e426b3a7ba97a87645859e05bcd4126
1 parent 87f5b8b commit 18f1c69

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed
 

‎RNTester/js/examples/PlatformColor/PlatformColorExample.js

+50-50
Original file line numberDiff line numberDiff line change
@@ -29,94 +29,94 @@ function PlatformColorsExample() {
2929
colors = [
3030
// https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors
3131
// Label Colors
32-
{label: 'labelColor', color: PlatformColor('labelColor')},
32+
{label: 'label', color: PlatformColor('label')},
3333
{
34-
label: 'secondaryLabelColor',
35-
color: PlatformColor('secondaryLabelColor'),
34+
label: 'secondaryLabel',
35+
color: PlatformColor('secondaryLabel'),
3636
},
3737
{
38-
label: 'tertiaryLabelColor',
39-
color: PlatformColor('tertiaryLabelColor'),
38+
label: 'tertiaryLabel',
39+
color: PlatformColor('tertiaryLabel'),
4040
},
4141
{
42-
label: 'quaternaryLabelColor',
43-
color: PlatformColor('quaternaryLabelColor'),
42+
label: 'quaternaryLabel',
43+
color: PlatformColor('quaternaryLabel'),
4444
},
4545
// Fill Colors
46-
{label: 'systemFillColor', color: PlatformColor('systemFillColor')},
46+
{label: 'systemFill', color: PlatformColor('systemFill')},
4747
{
48-
label: 'secondarySystemFillColor',
49-
color: PlatformColor('secondarySystemFillColor'),
48+
label: 'secondarySystemFill',
49+
color: PlatformColor('secondarySystemFill'),
5050
},
5151
{
52-
label: 'tertiarySystemFillColor',
53-
color: PlatformColor('tertiarySystemFillColor'),
52+
label: 'tertiarySystemFill',
53+
color: PlatformColor('tertiarySystemFill'),
5454
},
5555
{
56-
label: 'quaternarySystemFillColor',
57-
color: PlatformColor('quaternarySystemFillColor'),
56+
label: 'quaternarySystemFill',
57+
color: PlatformColor('quaternarySystemFill'),
5858
},
5959
// Text Colors
6060
{
61-
label: 'placeholderTextColor',
62-
color: PlatformColor('placeholderTextColor'),
61+
label: 'placeholderText',
62+
color: PlatformColor('placeholderText'),
6363
},
6464
// Standard Content Background Colors
6565
{
66-
label: 'systemBackgroundColor',
67-
color: PlatformColor('systemBackgroundColor'),
66+
label: 'systemBackground',
67+
color: PlatformColor('systemBackground'),
6868
},
6969
{
70-
label: 'secondarySystemBackgroundColor',
71-
color: PlatformColor('secondarySystemBackgroundColor'),
70+
label: 'secondarySystemBackground',
71+
color: PlatformColor('secondarySystemBackground'),
7272
},
7373
{
74-
label: 'tertiarySystemBackgroundColor',
75-
color: PlatformColor('tertiarySystemBackgroundColor'),
74+
label: 'tertiarySystemBackground',
75+
color: PlatformColor('tertiarySystemBackground'),
7676
},
7777
// Grouped Content Background Colors
7878
{
79-
label: 'systemGroupedBackgroundColor',
80-
color: PlatformColor('systemGroupedBackgroundColor'),
79+
label: 'systemGroupedBackground',
80+
color: PlatformColor('systemGroupedBackground'),
8181
},
8282
{
83-
label: 'secondarySystemGroupedBackgroundColor',
84-
color: PlatformColor('secondarySystemGroupedBackgroundColor'),
83+
label: 'secondarySystemGroupedBackground',
84+
color: PlatformColor('secondarySystemGroupedBackground'),
8585
},
8686
{
87-
label: 'tertiarySystemGroupedBackgroundColor',
88-
color: PlatformColor('tertiarySystemGroupedBackgroundColor'),
87+
label: 'tertiarySystemGroupedBackground',
88+
color: PlatformColor('tertiarySystemGroupedBackground'),
8989
},
9090
// Separator Colors
91-
{label: 'separatorColor', color: PlatformColor('separatorColor')},
91+
{label: 'separator', color: PlatformColor('separator')},
9292
{
93-
label: 'opaqueSeparatorColor',
94-
color: PlatformColor('opaqueSeparatorColor'),
93+
label: 'opaqueSeparator',
94+
color: PlatformColor('opaqueSeparator'),
9595
},
9696
// Link Color
97-
{label: 'linkColor', color: PlatformColor('linkColor')},
97+
{label: 'link', color: PlatformColor('link')},
9898
// Nonadaptable Colors
99-
{label: 'darkTextColor', color: PlatformColor('darkTextColor')},
100-
{label: 'lightTextColor', color: PlatformColor('lightTextColor')},
99+
{label: 'darkText', color: PlatformColor('darkText')},
100+
{label: 'lightText', color: PlatformColor('lightText')},
101101
// https://developer.apple.com/documentation/uikit/uicolor/standard_colors
102102
// Adaptable Colors
103-
{label: 'systemBlueColor', color: PlatformColor('systemBlueColor')},
104-
{label: 'systemBrownColor', color: PlatformColor('systemBrownColor')},
105-
{label: 'systemGreenColor', color: PlatformColor('systemGreenColor')},
106-
{label: 'systemIndigoColor', color: PlatformColor('systemIndigoColor')},
107-
{label: 'systemOrangeColor', color: PlatformColor('systemOrangeColor')},
108-
{label: 'systemPinkColor', color: PlatformColor('systemPinkColor')},
109-
{label: 'systemPurpleColor', color: PlatformColor('systemPurpleColor')},
110-
{label: 'systemRedColor', color: PlatformColor('systemRedColor')},
111-
{label: 'systemTealColor', color: PlatformColor('systemTealColor')},
112-
{label: 'systemYellowColor', color: PlatformColor('systemYellowColor')},
103+
{label: 'systemBlue', color: PlatformColor('systemBlue')},
104+
{label: 'systemBrown', color: PlatformColor('systemBrown')},
105+
{label: 'systemGreen', color: PlatformColor('systemGreen')},
106+
{label: 'systemIndigo', color: PlatformColor('systemIndigo')},
107+
{label: 'systemOrange', color: PlatformColor('systemOrange')},
108+
{label: 'systemPink', color: PlatformColor('systemPink')},
109+
{label: 'systemPurple', color: PlatformColor('systemPurple')},
110+
{label: 'systemRed', color: PlatformColor('systemRed')},
111+
{label: 'systemTeal', color: PlatformColor('systemTeal')},
112+
{label: 'systemYellow', color: PlatformColor('systemYellow')},
113113
// Adaptable Gray Colors
114-
{label: 'systemGrayColor', color: PlatformColor('systemGrayColor')},
115-
{label: 'systemGray2Color', color: PlatformColor('systemGray2Color')},
116-
{label: 'systemGray3Color', color: PlatformColor('systemGray3Color')},
117-
{label: 'systemGray4Color', color: PlatformColor('systemGray4Color')},
118-
{label: 'systemGray5Color', color: PlatformColor('systemGray5Color')},
119-
{label: 'systemGray6Color', color: PlatformColor('systemGray6Color')},
114+
{label: 'systemGray', color: PlatformColor('systemGray')},
115+
{label: 'systemGray2', color: PlatformColor('systemGray2')},
116+
{label: 'systemGray3', color: PlatformColor('systemGray3')},
117+
{label: 'systemGray4', color: PlatformColor('systemGray4')},
118+
{label: 'systemGray5', color: PlatformColor('systemGray5')},
119+
{label: 'systemGray6', color: PlatformColor('systemGray6')},
120120
];
121121
} else if (Platform.OS === 'android') {
122122
colors = [

‎React/Base/RCTConvert.m

+19-2
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ +(type)type : (id)json \
604604
{
605605
static NSDictionary<NSString *, NSDictionary *> *colorMap = nil;
606606
if (colorMap == nil) {
607-
colorMap = @{
607+
NSMutableDictionary<NSString *, NSDictionary *> *map = [@{
608608
// https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors
609609
// Label Colors
610610
@"labelColor" : @{
@@ -729,7 +729,22 @@ +(type)type : (id)json \
729729
// iOS 13.0
730730
RCTFallbackARGB : @(0xFFf2f2f7)
731731
},
732+
} mutableCopy];
733+
// The color names are the Objective-C UIColor selector names,
734+
// but Swift selector names are valid as well, so make aliases.
735+
static NSString *const RCTColorSuffix = @"Color";
736+
NSMutableDictionary<NSString *, NSDictionary *> *aliases = [NSMutableDictionary new];
737+
for (NSString *objcSelector in map) {
738+
RCTAssert([objcSelector hasSuffix:RCTColorSuffix], @"A selector in the color map did not end with the suffix Color.");
739+
NSMutableDictionary *entry = [map[objcSelector] mutableCopy];
740+
RCTAssert([entry objectForKey:RCTSelector] == nil, @"Entry should not already have an RCTSelector");
741+
NSString *swiftSelector = [objcSelector substringToIndex:[objcSelector length] - [RCTColorSuffix length]];
742+
entry[RCTSelector] = objcSelector;
743+
aliases[swiftSelector] = entry;
744+
}
745+
[map addEntriesFromDictionary:aliases];
732746
#if DEBUG
747+
[map addEntriesFromDictionary:@{
733748
// The follow exist for Unit Tests
734749
@"unitTestFallbackColor" : @{RCTFallback : @"gridColor"},
735750
@"unitTestFallbackColorIOS" : @{RCTFallback : @"blueColor"},
@@ -743,9 +758,11 @@ +(type)type : (id)json \
743758
RCTIndex : @1,
744759
RCTFallback : @"controlAlternatingRowBackgroundColors"
745760
},
761+
}];
746762
#endif
747-
};
763+
colorMap = [map copy];
748764
}
765+
749766
return colorMap;
750767
}
751768

0 commit comments

Comments
 (0)
Please sign in to comment.