问题描述
是否可以在uialertview中为某些特定的单词着色?
我的意思是,我可以将单词RED涂成红色,同时将GREEN涂成绿色吗?
is there a way to color some specific words in uialertview ?I mean, can I color the word RED as red and at the same time GREEN as green ?
有人可以用正确的方式带我走吗?
Can someone bring me on the right way?
推荐答案
您可以使用 NSAttributedString
和 UIAlertController
:
let strings = [["text" : "My String red\n", "color" : UIColor.redColor()], ["text" : "My string green", "color" : UIColor.greenColor()]];
var attributedString = NSMutableAttributedString()
for configDict in strings {
if let color = configDict["color"] as? UIColor, text = configDict["text"] as? String {
attributedString.appendAttributedString(NSAttributedString(string: text, attributes: [NSForegroundColorAttributeName : color]))
}
}
let alert = UIAlertController(title: "Title", message: "", preferredStyle: .Alert)
alert.setValue(attributedString, forKey: "attributedMessage")
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
presentViewController(alert,
animated: true,
completion: nil)
您可以使用键 attributedMessage
为消息分配属性字符串,并使用键 attributedTitle
作为标题。
You use the key attributedMessage
to assign an attributed string for the message and the key attributedTitle
for the title.
我找不到有效的 UIAlertView
解决方案但是它们都使用私有变量,这不是一个好习惯。您应该知道 UIAlertView
是iOS 8以后的版本,因此,如果您的目标不是较低版本,则不要使用它,而应使用 UIAlertController
I couldn't find a solution for UIAlertView
that works but all of them use private variables which is not good practice. You should know that UIAlertView
is deprecate from iOS 8 on, so if you are not targeting lower, you should not use it, instead use UIAlertController
这篇关于如何给uialertview或UIAlertController中的特定单词上色? (迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!