简体   繁体   English

在 Swift 中更改 UITextView 的 textColor

[英]Change textColor of a UITextView in Swift

I have an iOS project I'm working on using Xcode7 and Swift2 .我有一个正在使用Xcode7Swift2iOS项目。 I have a UITextView that I'm trying to change the textColor of.我有一个UITextView ,我正在尝试更改它的textColor It has a black background color.它具有黑色背景色。

I looked here and could not find a setTextColor .在这里看,找不到setTextColor I also tried below in the UIViewController of where the UITextView is located with no luck:我还在UITextView所在的UIViewController中尝试了以下方法,但没有成功:

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

    self.codeText.delegate = self

    codeText.textColor = UIColor.whiteColor()
}

I then went into the Inspector and tried clicking on the UITextView and changing the textColor there with no luck of it changing.然后我进入 Inspector 并尝试单击UITextView并在那里更改textColor但没有更改。 Am I doing something wrong?我做错了什么吗?

Thank you.谢谢。

on your ViewController remove the line在您的 ViewController 上删除该行

self.codeText.delegate = self

You can not assign a value of type "ViewController" to type "UITextViewDelegate?"您不能将“ViewController”类型的值分配给“UITextViewDelegate”类型吗?

codeText.textColor = UIColor.redColor()

works just fine.工作得很好。

You are sure that your codeText variale is not nil?您确定您的 codeText 变量不为零吗? in my opinion your code should work.在我看来,您的代码应该可以工作。 Please check wether codeText is not nil.请检查 codeText 是否不为零。

I ended up deleting the UITextField in the Storyboard and putting a new one.我最终删除了StoryboardUITextField并放置了一个新的。 I connected it to the ViewController with an @IBOutlet as before.我像以前一样使用@IBOutlet将它连接到ViewController I then went to 'Product' and 'Clean'.然后我去了“产品”和“清洁”。 Afterwards I ran my my project and it worked fine.之后我运行了我的项目,它运行良好。 I still had the newly added UITextView called codeText assigned to the UITextFieldDelegate and works beautifully.我仍然将新添加的名为codeText UITextView分配给UITextFieldDelegate并且工作得很好。 Must have just had a bug in it and needed to start fresh.一定只是有一个错误,需要重新开始。

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

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

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