1

I am making an iOS version of an Android app. I want to make the string I am passing through to be bold. In the other app it is done by adding html as shown below. I tried this and it did not work. Is there a way of implementing this or work around possibly?

Side note: showPrompt is a switch I made for UIAlertController functions.

Thanks,

func systemUpdateCheck(oCurVer: String){    

    showPrompt("A new version has been released,<b>v." + oCurVer + "<b>.<br/>Tap OK to download and install the new version", sType: "alert", sJSResp: "")
}
DG7
  • 161
  • 3
  • 15

3 Answers3

6

try this

let attributedText = NSMutableAttributedString(string: "Message", attributes: [NSAttributedStringKey.font: UIFont(name:".SFUIText-BoldItalic", size: 12)!])
let alert = UIAlertController(title: "Title", message: "", preferredStyle: .alert)
alert.setValue(attributedText, forKey: "attributedMessage")
let okAction = UIAlertAction(title: LocalizedString("Ok"), style: .default)
alert.addAction(okAction)
present(alert, animated: true)
Prateek kumar
  • 244
  • 3
  • 9
1

I know this question was asked a long time ago and we are now on iOS 16 & Swift 5.5 but I found this workable solution:-

let message = "This is a message with bold text"
let attributedString = NSMutableAttributedString(string: message)
let range = (message as NSString).range(of: "bold text")
attributedString.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 17), range: range)

let alert = UIAlertController(title: "Alert Title", message: "", preferredStyle: .alert)
alert.setValue(attributedString, forKey: "attributedMessage")
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)

enter image description here

Andy
  • 107
  • 1
  • 8
0

There's no public API to alter the text appearance in a UIAlertController.

The new UIAlertController is just a UIViewController subclass. You could create a custom UIViewController subclass and obtain a much more fine-grained control over its appearance and behaviour.

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40