单击按钮时设置所有 UI 按钮的标题颜色
Set title color of all UIbuttons on button click
我设置了按钮的标题颜色
button.setTitle("Print 0", for: UIControl.State.normal)
但是我有很多按钮我怎么能一次设置它设置半径和边框工作但是设置标题有一些问题
@objc extension UIButton {
dynamic var borderColor: UIColor? {
get {
if let cgColor = layer.borderColor {
return UIColor(cgColor: cgColor)
}
return nil
}
set { layer.borderColor = newValue?.cgColor }
}
dynamic var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
dynamic var cornerRadius: CGFloat {
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
dynamic var setTitleColor: UIButton? {
get {
if let cgColor = layer.color {
return UIColor(cgColor: cgColor)
}
return nil
}
set { layer.setTitleColor = newValue?.cgColor }
}
}
而不是 layer.color,使用 titleColor(for:)
和 setTitleColor(, for:)
,如下所示:
extension UIButton {
dynamic var normalTitleColor: UIColor? {
get {
return titleColor(for: .normal)
}
set {
setTitleColor(newValue, for: .normal)
}
}
}
我设置了按钮的标题颜色
button.setTitle("Print 0", for: UIControl.State.normal)
但是我有很多按钮我怎么能一次设置它设置半径和边框工作但是设置标题有一些问题
@objc extension UIButton {
dynamic var borderColor: UIColor? {
get {
if let cgColor = layer.borderColor {
return UIColor(cgColor: cgColor)
}
return nil
}
set { layer.borderColor = newValue?.cgColor }
}
dynamic var borderWidth: CGFloat {
get { return layer.borderWidth }
set { layer.borderWidth = newValue }
}
dynamic var cornerRadius: CGFloat {
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
dynamic var setTitleColor: UIButton? {
get {
if let cgColor = layer.color {
return UIColor(cgColor: cgColor)
}
return nil
}
set { layer.setTitleColor = newValue?.cgColor }
}
}
而不是 layer.color,使用 titleColor(for:)
和 setTitleColor(, for:)
,如下所示:
extension UIButton {
dynamic var normalTitleColor: UIColor? {
get {
return titleColor(for: .normal)
}
set {
setTitleColor(newValue, for: .normal)
}
}
}