如何更改一行中单个字母或一个单词的颜色
How to change color of single letters or one word in line
如何在 AlertView
中更改 "message" 中的字词颜色
@@IBAction func btn_instructions(sender: UIButton) {
let alertView = UNAlertView(title: "MyTitle", message: "Here green text, here red text")
alertView.show()
抱歉,如果问题不正确。
这对于 UIAlertController 是不可能的(UIAlertView 已弃用)。 UIAlertController 接受 title
和 message
参数的可选字符串(参见 Apple's Documentation on UIAlertController)。
为了在一行文本中使用多种颜色,您需要使用 NSAttributedString
(这里有一个很好的 NSAttributedString Tutorial)。
由于 String 不能包含任何属性,您将无法在 UIAlertController 上使用 NSAttributedString。您必须使用 NSAttributedString 标签创建自己的模式 ViewController 才能显示您的要求。
正如我在上面的评论中所写,UIAlertView
已被弃用,因此您必须改用 UIAlertController
。但是,您 可以 为消息设置属性字符串,使用 (non-swifty) key-value 编码(因为 UIAlertView
是 [=14 的子类=]): 为键 "attributedMessage"
设置属性字符串。标题的关联键是 "attributedTitle"
.
但是请注意,据我所知,这些功能似乎没有被 Apple 记录,仅由用户通过 runtime introspection.
引用。
示例如下:
import UIKit
class ViewController: UIViewController {
// ...
@IBAction func showAlert(sender: UIButton) {
let alertController = UIAlertController(title: "Foo", message: "", preferredStyle: UIAlertControllerStyle.Alert)
/* attributed string for alertController message */
let attributedString = NSMutableAttributedString(string: "Bar Bar Bar!")
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(),
range: NSRange(location:0,length:3))
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(),
range: NSRange(location:4,length:3))
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
range: NSRange(location:8,length:3))
alertController.setValue(attributedString, forKey: "attributedMessage")
/* action: OK */
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
生成以下结果:
如何在 AlertView
中更改 "message" 中的字词颜色@@IBAction func btn_instructions(sender: UIButton) {
let alertView = UNAlertView(title: "MyTitle", message: "Here green text, here red text")
alertView.show()
抱歉,如果问题不正确。
这对于 UIAlertController 是不可能的(UIAlertView 已弃用)。 UIAlertController 接受 title
和 message
参数的可选字符串(参见 Apple's Documentation on UIAlertController)。
为了在一行文本中使用多种颜色,您需要使用 NSAttributedString
(这里有一个很好的 NSAttributedString Tutorial)。
由于 String 不能包含任何属性,您将无法在 UIAlertController 上使用 NSAttributedString。您必须使用 NSAttributedString 标签创建自己的模式 ViewController 才能显示您的要求。
正如我在上面的评论中所写,UIAlertView
已被弃用,因此您必须改用 UIAlertController
。但是,您 可以 为消息设置属性字符串,使用 (non-swifty) key-value 编码(因为 UIAlertView
是 [=14 的子类=]): 为键 "attributedMessage"
设置属性字符串。标题的关联键是 "attributedTitle"
.
但是请注意,据我所知,这些功能似乎没有被 Apple 记录,仅由用户通过 runtime introspection.
引用。示例如下:
import UIKit
class ViewController: UIViewController {
// ...
@IBAction func showAlert(sender: UIButton) {
let alertController = UIAlertController(title: "Foo", message: "", preferredStyle: UIAlertControllerStyle.Alert)
/* attributed string for alertController message */
let attributedString = NSMutableAttributedString(string: "Bar Bar Bar!")
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(),
range: NSRange(location:0,length:3))
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(),
range: NSRange(location:4,length:3))
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
range: NSRange(location:8,length:3))
alertController.setValue(attributedString, forKey: "attributedMessage")
/* action: OK */
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
生成以下结果: