Instantly share code, notes, and snippets.
Last active
January 18, 2018 08:40
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save hmhmsh/6f746c2207c0631aee6d5076bcf200e7 to your computer and use it in GitHub Desktop.
Extension titleColor of UIButton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol ColorMaker { | |
associatedtype WillSetColorMaker | |
func color(_ color: UIColor) -> WillSetColorMaker | |
} | |
protocol ControlStateMaker { | |
associatedtype WillSetControlStateMaker | |
func controlState(_ controlState: UIControlState) -> WillSetControlStateMaker | |
} | |
protocol AllSetMaker { | |
} | |
struct InitializeMaker: ColorMaker { | |
func color(_ color: UIColor) -> DidSetColorMaker { | |
return DidSetColorMaker(color: color) | |
} | |
} | |
struct DidSetColorMaker: ControlStateMaker { | |
let color: UIColor | |
func controlState(_ controlState: UIControlState) -> DidAllSetMaker { | |
return DidAllSetMaker(color: color, controlState: controlState) | |
} | |
} | |
struct DidAllSetMaker: AllSetMaker { | |
let color: UIColor | |
let controlState: UIControlState | |
} | |
extension UIButton { | |
func titleColor(_ making:(InitializeMaker) -> DidAllSetMaker) { | |
let initializeMaker = InitializeMaker() | |
let allSetMaker = making(initializeMaker) | |
setTitleColor(allSetMaker.color, for: allSetMaker.controlState) | |
} | |
} | |
// Usage | |
let button = UIButton() | |
button.titleColor({ | |
$0.color(UIColor.red) | |
.controlState([.normal, .highlighted]) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UIControlStateの複数指定に未対応 | |
extension UIButton { | |
enum ColorControl { | |
case normal(UIColor) | |
case highlighted(UIColor) | |
case disabled(UIColor) | |
case selected(UIColor) | |
@available(iOS 9.0, *) | |
case focused(UIColor) | |
case application(UIColor) | |
case reserved(UIColor) | |
func getColor() -> UIColor? { | |
switch self { | |
case ColorControl.normal(let color): | |
return color | |
case ColorControl.highlighted(let color): | |
return color | |
case ColorControl.disabled(let color): | |
return color | |
case ColorControl.selected(let color): | |
return color | |
case ColorControl.focused(let color): | |
return color | |
case ColorControl.application(let color): | |
return color | |
case ColorControl.reserved(let color): | |
return color | |
} | |
} | |
func getControl() -> UIControlState { | |
switch self { | |
case ColorControl.normal(_): | |
return UIControlState.normal | |
case ColorControl.highlighted(_): | |
return UIControlState.highlighted | |
case ColorControl.disabled(_): | |
return UIControlState.disabled | |
case ColorControl.selected(_): | |
return UIControlState.selected | |
case ColorControl.focused(_): | |
return UIControlState.focused | |
case ColorControl.application(_): | |
return UIControlState.application | |
case ColorControl.reserved(_): | |
return UIControlState.reserved | |
} | |
} | |
} | |
func titleColor(_ colorControl: ColorControl) { | |
setTitleColor(colorControl.getColor(), for: colorControl.getControl()) | |
} | |
} | |
// Usage | |
let button = UIButton() | |
button.titleColor(.normal(UIColor.red)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage | |
let button = UIButton() | |
button.setTitleColor(UIColor.red, for: .normal) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ColorControl { | |
let color: UIColor | |
let controlState: UIControlState | |
} | |
extension UIButton { | |
var tt: ColorControl? { | |
get { | |
return nil | |
} | |
set(cc) { | |
if let colorControl = cc { | |
setTitleColor(colorControl.color, for: colorControl.controlState) | |
} | |
} | |
} | |
} | |
// Usage | |
let button = UIButton() | |
button.tt = ColorControl(color: .red, controlState: .normal) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ColorControl { | |
let color: UIColor | |
let controlState: UIControlState | |
} | |
extension UIButton { | |
func titleColor(_ colorControl: ColorControl) { | |
setTitleColor(colorControl.color, for: colorControl.controlState) | |
} | |
} | |
// Usage | |
let button = UIButton() | |
button.titleColor(ColorControl(color: .red, controlState: .normal)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment