You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is an Auto Layout convenience method for centering views.
135
+
136
+
```swift
137
+
// center a container view to its superview
138
+
let container =UIView()
139
+
addSubview(container)
140
+
141
+
container.constrainCenter()
142
+
143
+
// center a button horizontally
144
+
let button =UIButton()
145
+
addSubview(button)
146
+
147
+
button.constrainCenter(.x)
148
+
149
+
// align a button and a label vertically by their centers
150
+
let button =UIButton()
151
+
let label =UILabel()
152
+
addSubview(button)
153
+
addSubview(label)
154
+
155
+
button.constrainCenter(.y, to: label)
156
+
```
157
+
158
+
#### Constrain aspect ratio
159
+
160
+
There is an Auto Layout convenience method for constraining aspect ratio:
161
+
162
+
```swift
163
+
// constrain to a 16:9 aspect ratio
164
+
mediaPlayer.constrainAspectRatio(16.0/9)
165
+
166
+
// constrain to a 1:1 aspect ratio
167
+
profileImage.constrainAspectRatio(1)
168
+
```
169
+
170
+
### 2. Protocols to aid loading string, color, and image assets
171
+
172
+
We have extensions that accelerate loading strings, colors, and images (and make it easy to unit test them).
173
+
174
+
#### `Localizable`
175
+
176
+
Easily load localized string resources from any string-based `Enum`. All you need to do is declare conformance to `Localizable` and you gain access to a `localized: String` property.
The protocol also allows you to specify the bundle containing the localized strings and the optional table name.
205
+
206
+
#### `Colorable`
207
+
208
+
Easily load color assets from any string-based `Enum`. All you need to do is declare conformance to `Colorable` and you gain access to a `color: Color` property. You can even define a `fallbackColor` instead of `nil` or `.clear` so that UI elements won’t be invisible in the event of a failure (but they’re bright pink by default to catch your eye).
The protocol also allows you to specify the bundle containing the color assets, the optional namespace, and the fallback color.
232
+
233
+
#### `ImageAsset`
234
+
235
+
Easily load image assets from any string-based `Enum`. All you need to do is declare conformance to `ImageAsset` and you gain access to an `image: UIImage` property. You can even define a `fallbackImage` instead of `nil` so that UI elements won’t be invisible in the event of a failure (but it’s a bright pink square by default to catch your eye).
236
+
237
+
```swift
238
+
// Conform your Enum to ImageAsset
239
+
enumFlags: String, ImageAsset {
240
+
caseunitedStates="flag_us"
241
+
caseindia="flag_in"
242
+
}
243
+
244
+
let flag: Flags = .india
245
+
// Then access the image
246
+
let image: UIImage = flag.image
247
+
```
248
+
249
+
If you add `CaseIterable` to your enum, then it becomes super simple to write unit tests to make sure they’re working properly (and you can add, update, modify the enum cases without needing to update your unit test).
250
+
251
+
```swift
252
+
enumIcons: String, CaseIterable, ImageAsset {
253
+
casevalue1
254
+
casevalue2
255
+
...
256
+
casevalueLast
257
+
}
258
+
259
+
functest_iconsEnum_loadsImage() {
260
+
Icons.allCases.forEach {
261
+
XCTAssertNotNil($0.loadImage())
262
+
}
263
+
}
264
+
```
265
+
266
+
The protocol also allows you to specify the bundle containing the image assets, the optional namespace, and the fallback image.
267
+
268
+
#### `SystemImage`
269
+
270
+
Easily load system images (SF Symbols) from any string-based `Enum`. All you need to do is declare conformance to `SystemImage` and you gain access to an `image: UIImage` property. Like `ImageAsset` above, you can define a `fallbackImage`.
271
+
272
+
Why bother doing this when it just wraps `UIImage(systemName:)`? Because
273
+
1.`UIImage(systemName:)` returns `UIImage?` while `SystemImage.image` returns `UIImage`.
274
+
2. Organizing your system images into enums encourages better architecture (and helps avoid stringly-typed errors).
If you add `CaseIterable` to your enum, then it becomes super simple to write unit tests to make sure they’re working properly (and you can add, update, modify the enum cases without needing to update your unit test).
290
+
291
+
```swift
292
+
enumCheckbox: String, CaseIterable, SystemImage {
293
+
casechecked="checkmark.square"
294
+
caseunchecked="square"
295
+
}
296
+
297
+
functest_checkboxEnum_loadsImage() {
298
+
Checkbox.allCases.forEach {
299
+
XCTAssertNotNil($0.loadImage())
300
+
}
301
+
}
302
+
```
303
+
304
+
### 3. UIColor extensions for WCAG 2.0 contrast ratio calculations
114
305
115
306
Y—CoreUI contains a number of extensions to make working with colors easier. The most useful of them may be WCAG 2.0 contrast calculations. Given any two colors (representing foreground and background colors), you can calculate the contrast ration between them and evaluate whether that passes particular WCAG 2.0 standards (AA or AAA). You can even write unit tests to quickly check all color pairs in your app across all color modes. That could look like this:
116
307
@@ -185,7 +376,7 @@ final class ColorsTests: XCTestCase {
185
376
}
186
377
```
187
378
188
-
### 3. UIScrollView extensions to assist with keyboard avoidance
379
+
### 4. UIScrollView extensions to assist with keyboard avoidance
189
380
190
381
#### FormViewController
191
382
@@ -281,7 +472,7 @@ Prior to submitting a pull request you should:
281
472
282
473
When submitting a pull request:
283
474
284
-
* Use the [provided pull request template](PULL_REQUEST_TEMPLATE.md) and populate the Introduction, Purpose, and Scope fields at a minimum.
475
+
* Use the [provided pull request template](.github/pull_request_template.md) and populate the Introduction, Purpose, and Scope fields at a minimum.
285
476
* If you're submitting before and after screenshots, movies, or GIF's, enter them in a two-column table so that they can be viewed side-by-side.
0 commit comments