How to change color of word in "message" in AlertView
@@IBAction func btn_instructions(sender: UIButton) {
let alertView = UNAlertView(title: "MyTitle", message: "Here green text, here red text")
alertView.show()
Sorry if question is not correct.
This is not possible with UIAlertController (UIAlertView is deprecated). UIAlertController accepts an Optional String for the
title
andmessage
parameters (see Apple's Documentation on UIAlertController).In order to use multiple colors in a line of text, you'll need to use
NSAttributedString
(here's a good NSAttributedString Tutorial).Since String can't hold any attributes, you won't be able to use NSAttributedString on the UIAlertController. You will have to create your own modal ViewController with a NSAttributedString label in order to display what you are asking for.
As I've written in my comment above,
UIAlertView
is deprecated, so you'll have to useUIAlertController
instead. You can, however, set attributed strings for the message, using (non-swifty) key-value coding (sinceUIAlertView
is a subclass ofNSObject
): setting an attributed string for key"attributedMessage"
. The associated key for the title is"attributedTitle"
.Note, however, that these features seems---as far as I can find---undocumented by Apple, referenced only as derived by users via runtime introspection.
An example follows below:
Producing the following result: