繁体   English   中英

在 Swift 中更改 UITextView 的 textColor

[英]Change textColor of a UITextView in Swift

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

在这里看,找不到setTextColor 我还在UITextView所在的UIViewController中尝试了以下方法,但没有成功:

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

    self.codeText.delegate = self

    codeText.textColor = UIColor.whiteColor()
}

然后我进入 Inspector 并尝试单击UITextView并在那里更改textColor但没有更改。 我做错了什么吗?

谢谢。

在您的 ViewController 上删除该行

self.codeText.delegate = self

您不能将“ViewController”类型的值分配给“UITextViewDelegate”类型吗?

codeText.textColor = UIColor.redColor()

工作得很好。

您确定您的 codeText 变量不为零吗? 在我看来,您的代码应该可以工作。 请检查 codeText 是否不为零。

我最终删除了StoryboardUITextField并放置了一个新的。 我像以前一样使用@IBOutlet将它连接到ViewController 然后我去了“产品”和“清洁”。 之后我运行了我的项目,它运行良好。 我仍然将新添加的名为codeText UITextView分配给UITextFieldDelegate并且工作得很好。 一定只是有一个错误,需要重新开始。

你需要用 .foregroundColor: UIColor.label 添加 NSAttributedString

暂无
暂无

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

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