简体   繁体   English

无法将“颜色”类型的值分配给“UIColor”类型

[英]Cannot assign value of type 'Color' to type 'UIColor'

When I set textField.textColor = Color.primary inside UITextViewWrapper class give this error.当我在UITextViewWrapper class 中设置textField.textColor = Color.primary时,会出现此错误。

How can I use primary color inside UITextViewWrapper?如何在 UITextViewWrapper 中使用原色? Is there any way to use it?有什么方法可以使用它吗?

fileprivate struct UITextViewWrapper: UIViewRepresentable {
    typealias UIViewType = UITextView

    func makeUIView(context: UIViewRepresentableContext<UITextViewWrapper>) -> UITextView {
        let textField = UITextView()
        textField.textColor = Color.primary // here give error
        return textField
    }
          
 }

On iOS 14+ you can use the new UIColor initializer.在 iOS 14+ 上,您可以使用新的UIColor初始化程序。

textField.textColor = UIColor(Color.primary)

For lower versions, you can use:对于较低版本,您可以使用:

textField.textColor = UIColor.label // gives the same result
  1. Extension UIColor:扩展 UIColor:

     extension UIColor { class var primary: UIColor { return UIColor(named: "YourColorName")! } }
  2. Usage:用法:

     textField.textColor =.primary

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

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