简体   繁体   English

如何在swift 4中以编程方式更改标签颜色

[英]How to change label colors programmatically in swift 4

When I change the label color on my display programmatically, but that doesn't really change the color of the label in my simulator当我以编程方式更改显示器上的标签颜色时,但这并没有真正改变模拟器中标签的颜色

this is my code :这是我的代码:

This code works for iOS v13.3, but not for iOS v12.4此代码适用于 iOS v13.3,但不适用于 iOS v12.4

lblClockOut.text = "text"

lblClockOut.textColor = UIColor(named: "Green-9920")

is there any other way?还有其他方法吗?

before that I say thank you :)在此之前我说谢谢:)

您是否尝试过在主线程上更新 UI,例如,

DispatchQueue.main.async{ self.lblClockOut.textColor = UIColor(named: "Green-9920") }

In my projects I would apply colors like this:在我的项目中,我会应用这样的颜色:

extension UIColor {

    static let green_9920 = #colorLiteral(red: 0.0862745098, green: 0.8509803922, blue: 0.4470588235, alpha: 1)

}

// Usage example:

lblClockOut.textColor = .green_9920

I've never had issues with simulators so maybe this will work for you.我从来没有遇到过模拟器问题,所以也许这对你有用。 Your code seems fine at first glance so not sure why its not working but you could try resetting the iOS 12 simulator and maybe its something to do with the referencing of your custom color.您的代码乍一看似乎很好,所以不确定为什么它不起作用,但您可以尝试重置 iOS 12 模拟器,也许它与您的自定义颜色的引用有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM