UIAlertController设置富文本消息

21
我们如何在UIAlertController中设置带属性的文本作为消息。以下是我的代码尝试,但它会导致应用程序崩溃。请参考以下内容:

如何在UIAlertController中设置带属性的文本作为消息。以下是我尝试过的代码,但它会导致应用程序崩溃。

// 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的代码

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)
谢谢。

输入图像描述


1
可能是UIAlertController自定义字体、大小、颜色的重复问题。 - Jakub Truhlář
你不能在不使用私有API的情况下将attributedString传递给UIAlertController - Jakub Truhlář
3个回答

41

你的应用程序崩溃是因为数据类型不匹配。

alert.setValue(<value>, forKey: "attributedMessage")

这里的 <value> 必须是 NSMutableAttributedString 的实例。

但是你正在传递一个名为 myAttribute 的 Dictionary

它试图调用 length 方法,但是在 Dictionary 上找不到该方法,因此导致应用程序崩溃。

尝试这个:

alert.setValue(myString, forKey: "attributedMessage")

感谢@CRDave的帮助。 - jignesh Vadadoriya
1
谢谢。这是令人担忧的,因为它没有明确地暴露出来,你首先必须盲目地执行这个操作。:-[ - Anton Tropashko
2
已向苹果申请51171348号文件,以消除此临时解决方案的需要。 - Anton Tropashko

2

这里的被接受答案在iOS 15上有效,但如果苹果公司改变了他们的私有API,这个方法将会毫无预警地崩溃。具有讽刺意味的是,历史上当他们将这种API公开化时,它才会发生变化并破坏像这样的事情。以下是一种安全使用私有API的方法:

if
    let property = class_getProperty(type(of: alert), "attributedMessage"),
    let type = property_copyAttributeValue(property, "T"),
    String(cString: type) == #"@"NSAttributedString""#
{
    alert.setValue(myString, forKey: "attributedMessage")
} else {
    alert.message = myString.string
}

0
作为私有API的替代方案,您可以使用https://github.com/AntonPoltoratskyi/NativeUI pod 'NativeUI/Alert', '~> 1.0' 它是一个从头开始实现并且外观与本地UIAlertController完全相同的自定义视图控制器。
您可以传递StringNSAttributedString或自定义内容视图。
let viewModel = Alert(
    title: "Your Title",
    message: nil,
    contentView: customView,
    actions: [cancelAction, confirmAction]
)
let alert = AlertViewController(viewModel: viewModel)
present(alert, animated: true)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接