UIAlertcontroller 设置属性消息

UIAlertcontroller set attributed messages

我们如何将 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)

谢谢。

您的应用程序因数据类型不匹配而崩溃。

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

此处<value>必须是NSMutableAttributedString的实例。

但是您传递的是 Dictionary 的 myAttribute。

它正在尝试调用 length 方法,但在 Dictionary 上找不到它,这就是应用程序崩溃的原因。

试试这个:

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

作为私有 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)

此处接受的答案自 iOS 15 起有效,但如果 Apple 更改其私有 api,这将在没有警告的情况下崩溃。具有讽刺意味的是,从历史上看,正是当他们制作这种 api public 时,它才会改变并破坏这样的东西。这是一种安全使用私有 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
}