0

I am trying to set an attributed message on an MDCAlertController that contains a word with bold text. However, it seems that the Material Components library on iOS ignores it and I'm not sure why.

Here is the code I am using and below I've provided a screenshot of the result.

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)

enter image description here

dcaraujo
  • 55
  • 1
  • 5

1 Answers1

0

A colleague of mine had a look and managed to figure out the issue. Turns out you just need to set messageFont. Here is the full example:

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)

enter image description here

However, considering that Google is deprecating Material Components iOS, rather use UIAlertController.

dcaraujo
  • 55
  • 1
  • 5