当试图在UIAlertController中更改文本的颜色时,我会得到下面描述的错误
由于未识别的异常“NSInvalidArgumentException”终止应用程序,原因:'-NSConcreteAttributedString rangeOfCharacterFromSet::未识别的选择器发送到实例0x60000003e320‘
函数,该函数试图更改颜色:
@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")
})
}如果我删除第5行,这个问题就解决了。NSForegroundColorAttributeName是一个有效的NSAttributedString-属性,所以我不理解错误。
发布于 2017-02-22 12:46:56
在message类中没有可用的密钥名为UIAlertController的属性,在这里使用attributedMessage来更改消息。
在这个地方
let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "message")使用
let alertController = UIAlertController.init(title: "Submit score", message: mess, preferredStyle: .alert)
alertController.setValue(NSAttributedString(string: mess, attributes: [NSForegroundColorAttributeName: UIColor.red]), forKey: "attributedMessage")如果您想更改UIAlertController的标题,那么您应该使用'attributedTitle‘键。
你得到的输出

发布于 2017-02-22 12:39:50
@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")
}发布于 2017-02-22 12:41:03
您似乎试图将NSAttributedString设置为字符串变量message。试试https://stackoverflow.com/a/42118706/7064692
https://stackoverflow.com/questions/42391568
复制相似问题