1

I get the error described under when trying to change color on the text inside an UIAlertController:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteAttributedString rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x60000003e320'

Function that tries to change the color:

@objc private func submitScore(color: Bool = false) {
    var mess = ""
    mess = color ? "Invalid username" : ""
    let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
    alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")


    //Submit button calls function again
    let submit = UIAlertAction(title: "Submit", style: .default) { (submit) in
        self.submitScore(color: true)
    }
    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (cancel) in

    })
    alertController.addTextField(configurationHandler: {(textfield) in
        textfield.placeholder = "Nickname"
    })
    alertController.addAction(submit)
    alertController.addAction(cancel)

    present(alertController, animated: true, completion: {(alertController) in
        print("shown")
    })
}

The problem is solved if i remove line 5. NSForegroundColorAttributeName is a valid NSAttributedString-property so I don't understand the error..

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
André
  • 143
  • 1
  • 14
  • The property `message` is of type `String` so you can not add any attributes to it. There is no exposed property for setting an attributed message. – rckoenes Feb 22 '17 at 12:43

3 Answers3

2

there is no property available of key name is message in UIAlertController class, in this place use attributedMessage for change the message.

in this place

   let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
  alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")

use

let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "attributedMessage")

and if you want to change the title of UIAlertController then you should use 'attributedTitle' key.

you get the output of

enter image description here

Nikunj Damani
  • 753
  • 3
  • 11
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0
 @objc private func submitScore(color: Bool = false) {
    var mess = ""
    mess = color ? "Invalid username" : ""
    let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)

    //Submit button calls function again
    let submit = UIAlertAction(title: "Submit", style: .default) { (submit) in
        self.submitScore(color: true)
    }
    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (cancel) in

    })
    alertController.addTextField(configurationHandler: {(textfield) in
        textfield.placeholder = "Nickname"
    })
    alertController.addAction(submit)
    alertController.addAction(cancel)

    present(alertController, animated: true, completion: {(alertController) in
        print("shown")
    })

    alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")

}
0

It seems you try to set NSAttributedString to a String variable message. Try https://stackoverflow.com/a/42118706/7064692

Community
  • 1
  • 1