swift 扩展名称UIButton的颜色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 扩展名称UIButton的颜色相关的知识,希望对你有一定的参考价值。

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))
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)
// Usage
let button = UIButton()
button.setTitleColor(UIColor.red, for: .normal)
// 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))
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])
})

以上是关于swift 扩展名称UIButton的颜色的主要内容,如果未能解决你的问题,请参考以下文章

swift-Tableview 单元格 UIButton 背景颜色

Swift UIButton 子类并根据变量更改颜色

在 Swift 中按任意键更改自定义 UIButton 的 BG 颜色

在 Swift 中按下 uibutton 时更改色调图像颜色

在 Swift (tvOS) 中,如何更改 UIButton 的突出显示和焦点颜色?

在 Swift 3 中突出显示(或点击)按钮时如何更改 UIButton 边框颜色?