├── LICENSE ├── README.md └── UIView-Styling.playground ├── Contents.swift ├── contents.xcplayground └── playground.xcworkspace └── contents.xcworkspacedata /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Marin Benčević 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UIView-Styling 2 | A Swift playground displaying a simple, type-safe, composable way to style UIViews in an iOS App. 3 | 4 | More info in [this article](https://medium.cobeisfresh.com/composable-type-safe-uiview-styling-with-swift-functions-8be417da947f#.wv69okrwu). 5 | -------------------------------------------------------------------------------- /UIView-Styling.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | 6 | /// An abstraction if `UIView` styling. 7 | struct UIViewStyle { 8 | 9 | /// The styling function that takes a `UIView` instance 10 | /// and performs side-effects on it. 11 | let styling: (T)-> Void 12 | 13 | /// A factory method that composes multiple styles. 14 | /// 15 | /// - Parameter styles: The styles to compose. 16 | /// - Returns: A new `UIViewStyle` that will call the input styles' 17 | /// `styling` method in succession. 18 | static func compose(_ styles: UIViewStyle...)-> UIViewStyle { 19 | 20 | return UIViewStyle { view in 21 | for style in styles { 22 | style.styling(view) 23 | } 24 | } 25 | } 26 | 27 | /// Compose this style with another. 28 | /// 29 | /// - Parameter other: Other style to compose this style with. 30 | /// - Returns: A new `UIViewStyle` which will call this style's `styling`, 31 | /// and then the `other` style's `styling`. 32 | func composing(with other: UIViewStyle)-> UIViewStyle { 33 | return UIViewStyle { view in 34 | self.styling(view) 35 | other.styling(view) 36 | } 37 | } 38 | 39 | /// Compose this style with another styling function. 40 | /// 41 | /// - Parameter otherStyling: The function to compose this style with. 42 | /// - Returns: A new `UIViewStyle` which will call this style's `styling`, 43 | /// and then the input `styling`. 44 | func composing(with otherStyling: @escaping (T)-> Void)-> UIViewStyle { 45 | return self.composing(with: UIViewStyle(styling: otherStyling)) 46 | } 47 | 48 | 49 | /// Apply this style to a UIView. 50 | /// 51 | /// - Parameter view: the view to style 52 | func apply(to view: T) { 53 | styling(view) 54 | } 55 | 56 | 57 | /// Apply this style to multiple views. 58 | /// 59 | /// - Parameter views: the views to style 60 | func apply(to views: T...) { 61 | for view in views { 62 | styling(view) 63 | } 64 | } 65 | } 66 | 67 | // Creating a new style: 68 | 69 | let smallLabelStyle: UIViewStyle = UIViewStyle { label in 70 | label.font = label.font.withSize(12) 71 | } 72 | 73 | let lightLabelStyle: UIViewStyle = UIViewStyle { label in 74 | label.textColor = .lightGray 75 | } 76 | 77 | // Creating a new style by composing two different styles: 78 | 79 | let captionLabelStyle: UIViewStyle = .compose(smallLabelStyle, lightLabelStyle) 80 | 81 | // Alternative way: 82 | 83 | let otherCaptionLabelStyle = smallLabelStyle.composing(with: lightLabelStyle) 84 | 85 | // You can also create a style by adding composing an existing 86 | // style with another styling function: 87 | 88 | let darkCaptionLabelStyle: UIViewStyle = captionLabelStyle.composing { label in 89 | label.textColor = .darkGray 90 | } 91 | 92 | // Styling a view in a `UIViewController`: 93 | 94 | class ViewController: UIViewController { 95 | 96 | let captionLabel = UILabel() 97 | 98 | override func viewDidLoad() { 99 | super.viewDidLoad() 100 | 101 | captionLabelStyle.apply(to: captionLabel) 102 | } 103 | 104 | } -------------------------------------------------------------------------------- /UIView-Styling.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /UIView-Styling.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | --------------------------------------------------------------------------------