0

我正在尝试在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)

在此处输入图像描述

4

1 回答 1

0

我的一个同事看了看,并设法弄清楚了这个问题。原来你只需要设置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.

于 2021-10-14T13:54:04.080 回答