UIAlertcontroller设置属性消息 [英] UIAlertcontroller set attributed messages
本文介绍了UIAlertcontroller设置属性消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在UIAlertcontroller中将属性文本设置为消息. 我的尝试代码如下,但是它将使应用程序崩溃.
How we can set Attributed text in UIAlertcontroller as message. My try code As bellow, but it will crash the app.
// create Attributed text
let myAttribute = [NSForegroundColorAttributeName: UIColor(red:122.0/255, green:125.0/255, blue:131.0/255, alpha:1.0),NSFontAttributeName: Constant.flinntRegularFont(15)]
let myAttribute2 = [NSForegroundColorAttributeName: UIColor.blackColor(),NSFontAttributeName: Constant.flinntMediumFont(15)]
let myString = NSMutableAttributedString(string: "You have been unsubscribed from ", attributes: myAttribute)
let myString2 = NSMutableAttributedString(string: self.course.course_name, attributes: myAttribute2)
let myString3 = NSMutableAttributedString(string: "\n\nYour refund will be initiated within one week.", attributes: myAttribute)
let myString4 = NSMutableAttributedString(string: "\n\nFor any help call us on", attributes: myAttribute)
let myString5 = NSMutableAttributedString(string: " 079-4014 9800", attributes: myAttribute2)
let myString6 = NSMutableAttributedString(string: " between 9:30 am to 6:30 pm on Monday to Saturday.\n\nWe will be always here with great deals to share.", attributes: myAttribute)
myString.appendAttributedString(myString2)
myString.appendAttributedString(myString3)
myString.appendAttributedString(myString4)
myString.appendAttributedString(myString5)
myString.appendAttributedString(myString6)
当前的UIAlertcontroller代码
Present UIAlertcontroller Code
let alert = UIAlertController(title: "", message: "Select course", preferredStyle: UIAlertControllerStyle.Alert)
alert.setValue(myAttribute, forKey: "attributedMessage") // this line make a crash.
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: { (action) in
self.delegate?.courseRefundViewControllerCoursrRefunded?(self)
self.navigationController?.popViewControllerAnimated(true)
}))
self.presentViewController(alert, animated: true, completion: nil)
谢谢.
推荐答案
您的应用由于数据类型不匹配而崩溃.
You app is crashing because of DataType mismatch.
alert.setValue(<value>, forKey: "attributedMessage")
此处<value>
必须是NSMutableAttributedString
的实例.
但是您传递的是Dictionary
的myAttribute.
But you are passing myAttribute Which is Dictionary
.
正在尝试调用length
方法,但是在Dictionary
上找不到它,这就是应用崩溃的原因.
It is trying ta call length
method but it is not found on Dictionary
thats why app is crashing.
尝试一下:
alert.setValue(myString, forKey: "attributedMessage")
这篇关于UIAlertcontroller设置属性消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文