在 Swift 中更改 UITextView 的文本颜色

Change textColor of a UITextView in Swift

我有一个 iOS 项目正在使用 Xcode7Swift2。我有一个 UITextView,我正在尝试更改 textColor 的。它有一个黑色的背景颜色。

我查看了 here,但找不到 setTextColor。我还在 UITextView 所在的 UIViewController 中尝试了以下但没有运气:

@IBOutlet weak var codeText: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()

    self.codeText.delegate = self

    codeText.textColor = UIColor.whiteColor()
}

然后我进入检查器并尝试单击 UITextView 并更改 textColor 那里没有运气改变。我做错了什么吗?

谢谢。

您确定您的代码 Text 变量不为 nil 吗?在我看来你的代码应该工作。请检查codeText是否不为nil。

在您的 ViewController 上删除行

self.codeText.delegate = self

您不能将类型 "ViewController" 的值分配给类型 "UITextViewDelegate?"

codeText.textColor = UIColor.redColor()

工作正常。

我最终删除了 Storyboard 中的 UITextField 并添加了一个新的。我像以前一样用 @IBOutlet 将它连接到 ViewController。然后我去了 'Product' 和 'Clean'。之后,我 运行 我的项目运行良好。我仍然将新添加的 UITextView 称为 codeText 分配给了 UITextFieldDelegate,并且工作得很好。一定是刚刚有一个错误,需要重新开始。

您需要添加带有 .foregroundColor 的 NSAttributedString:UIColor.label