如何在uialertview或UIAlertController中为特定单词着色? (迅速)

是否有办法为uialertview中的某些特定单词着色? 我的意思是,我可以将RED这个词的颜色设为红色,同时将GREEN设为绿色吗?

在此处输入图像描述

有人能以正确的方式带我吗?

您可以使用NSAttributedStringUIAlertController

  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

我找不到适用于UIAlertView的解决方案,但所有这些解决方案都使用私有变量,这不是一个好习惯。 你应该知道UIAlertView是从iOS 8开始弃用的,所以如果你没有降低目标,你不应该使用它,而是使用UIAlertController