【问题标题】:How do I set a bold attribute to a message on MDCAlertController?如何为 MDCAlertController 上的消息设置粗体属性?
【发布时间】:2021-09-27 16:22:23
【问题描述】:

我正在尝试在 MDCAlertController 上设置一个属性消息,其中包含一个带有粗体文本的单词。但是,iOS 上的 Material Components 库似乎忽略了它,我不知道为什么。

这是我正在使用的代码,下面我提供了结果的屏幕截图。

let message = "This should be bold"
let attributedString = NSMutableAttributedString(string: message, attributes: [
    .font: UIFont.systemFont(ofSize: 14)
])
attributedString.addAttribute(
    .font,
    value: UIFont.boldSystemFont(ofSize: 14),
    range: (message as NSString).range(of: "bold")
)

let alert = MDCAlertController(
    title: "Example alert",
    attributedMessage: attributedString
)
alert.addAction(MDCAlertAction(title: "OK"))
present(alert, animated: true)

【问题讨论】:

    标签: ios swift material-components-ios


    【解决方案1】:

    我的一位同事看了一眼并设法找出了问题所在。原来你只需要设置messageFont。这是完整的示例:

    let message = "This should be bold"
    let attributedString = NSMutableAttributedString(
        string: message, 
        attributes: [
            .font: UIFont.systemFont(ofSize: 14)
        ]
    )
    attributedString.addAttribute(
        .font,
        value: UIFont.boldSystemFont(ofSize: 14),
        range: (message as NSString).range(of: "bold")
    )
    let alert = MDCAlertController(
        title: "Example alert",
        attributedMessage: attributedString
    )
    alert.messageFont = UIFont.boldSystemFont(ofSize: 14)
    alert.addAction(MDCAlertAction(title: "OK"))
    present(alert, animated: true)
    

    但是,考虑到 Google 正在弃用 iOS 的 Material Components,不如使用 UIAlertController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2017-10-23
      • 2018-04-05
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2012-10-29
      相关资源
      最近更新 更多