├── LICENSE ├── README.md ├── TintingButton.swift └── previews ├── screenshot1.jpg └── screenshot2.jpg /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Dominik Veselý 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TintingButton 2 | 3 | Simple UIButton subclass which uses tintColor to color button elements (Title,Image,Border) based on current state. 4 | 5 | Why? 6 | --- 7 | 8 | UIButton provides functionality to change Title color per UIButton state. You can also change image per state. But since iOS7 introduced renderingmode option to UIImage and face it ... you most likely want just a different color of image than different image per state. 9 | 10 | TintColor property can color buttons image (when used as render template) but unfortunately tintColor does not change with UIButton's state. Thats why I decided to create TintingButton 11 | 12 | CocoaPods 13 | --- 14 | Currently not supported (or needed more likely) 15 | 16 | 17 | Usage 18 | --- 19 | 20 | TintingButton could be instantiated with "Title-Image-Border", "Title-Image" or Custom colored mode 21 | 22 | 23 | 24 | ###All Colored mode 25 | 26 | [] 27 | ~~~swift 28 | let btnShare = TintingButton(completelyTintedWith: .whiteColor(), activeTintColor: .greenColor()) 29 | btnShare.setImage(imageShare, forState: UIControlState.Normal) 30 | btnShare.setTitle(title, forState: UIControlState.Normal) 31 | 32 | ~~~ 33 | ###Title Image Colored mode 34 | 35 | [] 36 | 37 | 38 | ~~~swift 39 | let btnShare = TintingButton(titleAndImageTintedWith: whiteColor(), activeTintColor: .greenColor()) 40 | btnShare.setImage(imageShare, forState: UIControlState.Normal) 41 | btnShare.layer.borderColor = .grayColor().CGColor 42 | btnShare.layer.borderWidth = 1 43 | btnShare.setTitle(title, forState: UIControlState.Normal) 44 | 45 | ~~~ 46 | ###Custom colored mode 47 | 48 | TintingButton has closure syntax api to define which element should be tinted and which not. Thats in case you want to include some complex login. 49 | 50 | ~~~swift 51 | //Same as AllColored mode 52 | let btnShare = TintingButton(tintColor: .whiteColor(), activeTintColor: .greenColor()) { (control) -> Bool in return true} 53 | 54 | //Custom mode with your specific logic 55 | let btnShare = TintingButton(tintColor: .whiteColor(), activeTintColor: .greenColor()) { (control) -> Bool in 56 | 57 | switch control { 58 | case .Title,.Image : return yourComputedValue 59 | case .Border: return anotherComputedValue 60 | } 61 | } 62 | ~~~ 63 | -------------------------------------------------------------------------------- /TintingButton.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TintingButton.swift 3 | // 4 | // Created by Dominik Vesely on 11/04/15. 5 | // Copyright (c) 2015 Ackee s.r.o. All rights reserved. 6 | // 7 | 8 | import Foundation 9 | 10 | class TintingButton : UIButton { 11 | 12 | typealias TintingButtonAction = (control: Element) -> Bool 13 | var activeTintColor : UIColor? 14 | var normalTintColor : UIColor? 15 | 16 | private var action : TintingButtonAction? 17 | 18 | enum Element: Int { 19 | case Title,Image,Border 20 | } 21 | 22 | 23 | convenience init (completelyTintedWith tintColor : UIColor?, activeTintColor: UIColor?) { 24 | self.init(tintColor: tintColor, activeTintColor: activeTintColor, tintAction: {(control: Element) -> Bool in return true}) 25 | } 26 | 27 | 28 | convenience init (titleAndImageTintedWith tintColor : UIColor?, activeTintColor: UIColor?) { 29 | 30 | let defaultAction = { (control: Element) -> Bool in 31 | 32 | switch control { 33 | case .Title,.Image : return true 34 | case .Border: return false 35 | } 36 | } 37 | 38 | self.init(tintColor: tintColor, activeTintColor: activeTintColor, tintAction: defaultAction) 39 | 40 | 41 | } 42 | 43 | required init (tintColor : UIColor?, activeTintColor: UIColor?, tintAction: TintingButtonAction ) { 44 | super.init(frame: CGRect.zeroRect) 45 | 46 | self.normalTintColor = tintColor 47 | self.activeTintColor = activeTintColor 48 | self.tintColor = normalTintColor 49 | self.adjustsImageWhenHighlighted = false 50 | self.action = tintAction 51 | 52 | 53 | styleTitle() 54 | styleBorder() 55 | 56 | } 57 | 58 | 59 | required init(coder aDecoder: NSCoder) { 60 | super.init(coder: aDecoder) 61 | } 62 | 63 | 64 | func styleTitle() { 65 | 66 | if action!(control: .Title) { 67 | setTitleColor(tintColor, forState: UIControlState.Normal) 68 | setTitleColor(activeTintColor, forState: UIControlState.Selected) 69 | setTitleColor(activeTintColor, forState: UIControlState.Highlighted) 70 | } 71 | 72 | } 73 | 74 | func styleBorder() { 75 | if action!(control: .Border) { 76 | layer.borderColor = tintColor?.CGColor 77 | layer.borderWidth = layer.borderWidth == 0 ? 1.0 : layer.borderWidth 78 | } 79 | } 80 | 81 | override func setImage(image: UIImage?, forState state: UIControlState) { 82 | 83 | var img = image 84 | if action!(control: .Image) { 85 | img = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 86 | } 87 | 88 | super.setImage(img, forState: state) 89 | } 90 | 91 | 92 | 93 | override var selected : Bool { 94 | didSet { 95 | if selected { 96 | tintColor = activeTintColor 97 | } else { 98 | tintColor = normalTintColor 99 | } 100 | styleBorder() 101 | } 102 | } 103 | 104 | override var highlighted : Bool { 105 | didSet { 106 | if highlighted { 107 | tintColor = activeTintColor 108 | } else { 109 | tintColor = normalTintColor 110 | } 111 | styleBorder() 112 | 113 | } 114 | } 115 | 116 | 117 | } 118 | -------------------------------------------------------------------------------- /previews/screenshot1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ackdom/TintingButton/322dbf2e60dec97b4c8306d913376066abdf59cc/previews/screenshot1.jpg -------------------------------------------------------------------------------- /previews/screenshot2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ackdom/TintingButton/322dbf2e60dec97b4c8306d913376066abdf59cc/previews/screenshot2.jpg --------------------------------------------------------------------------------