【问题标题】:UIAlertcontroller set attributed messagesUIAlertcontroller 设置属性消息
【发布时间】:2017-10-23 03:26:18
【问题描述】:

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

谢谢。

【问题讨论】:

标签: ios iphone swift swift2.3


【解决方案1】:

此处接受的答案适用于 iOS 15,但如果 Apple 更改其私有 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
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      由于数据类型不匹配,您的应用程序正在崩溃。

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

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

      但是你传递的是 Dictionary 的 myAttribute。

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

      试试这个:

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

      【讨论】:

      • 感谢@CRDave 的帮助。
      • 谢谢。令人担忧的是,这没有明确暴露,你必须首先做这种盲目的飞行舞蹈:-[
      • 向苹果提交了 51171348 以消除对这种组合的需要
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多