gpt4 book ai didi

swift - 找出 uibutton 内标题的颜色

转载 作者:行者123 更新时间:2023-11-30 12:26:41 25 4
gpt4 key购买 nike

您可以通过执行setTitleColor(.white, for: .normal)来设置按钮的颜色,这会将按钮设置为白色。但是如果想检查按钮内标题的颜色怎么办?

我试过了let color = titleColor(for: .normal)let color = titleLabel?.textColor 但当我尝试在其他地方设置该颜色时,什么也没有发生。

用途:

extension UIButton {

func loadingIndicator(show: Bool) {
let tag = 9876

var color: UIColor?

if show {
color = titleColor(for: .normal)
let indicator = UIActivityIndicatorView()
let buttonHeight = self.bounds.size.height
let buttonWidth = self.bounds.size.width
indicator.center = CGPoint(x: buttonWidth/2, y: buttonHeight/2)
indicator.tag = tag
indicator.color = UIColor.white
setTitleColor(.clear, for: .normal)

self.addSubview(indicator)
indicator.startAnimating()
} else {
if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
indicator.stopAnimating()
indicator.removeFromSuperview()
setTitleColor(color, for: .normal)
}
}
}
}

最佳答案

这就是您如何通过 state 获取 UIButton 标题的颜色,并且可以在任何您想要的地方使用它。

let color = btn.titleColor(for: .normal);
label.textColor = color

正如您所看到的,我的按钮颜色在此图像中的标签文本颜色中正确使用。 enter image description here

这就是您在扩展中执行此操作的方式

extension UIButton {

func loadingIndicator(show: Bool) {
let tag = 9876

var color: UIColor?

if show {
color = titleColor(for: .normal)
let indicator = UIActivityIndicatorView()
let buttonHeight = self.bounds.size.height
let buttonWidth = self.bounds.size.width
indicator.center = CGPoint(x: buttonWidth/2, y: buttonHeight/2)
indicator.tag = tag
indicator.color = UIColor.white
setTitleColor(.clear, for: .normal)
self.addSubview(indicator)
indicator.startAnimating()
} else {
if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
indicator.stopAnimating()
indicator.removeFromSuperview()
setTitleColor(color, for: .normal)
}

}
}
}

像这样使用它:

//This will set the saved color for `UIButton` title
btn.loadingIndicator(show: false)

关于swift - 找出 uibutton 内标题的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44118249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com