I\'m working with Swift in iOS 9. I want to customize the font for a UIAlertAction
. I searched these questions for a Swift answer:
Change the Font of UI
You would use NSAttributedString
(or its mutable counterpart, NSMutableAttributedString
) to make a string with custom info. As the constructor takes an NSString
(or String
in Swift), it will not take an NSAttributedString
(NSAttributedString
isn't a subclass of NSString
.).
You could fudge the Swift compiler into passing an NSAttributedString
using unsafeBitCast
, but UIAlertAction
may not like it.
Looking at UIAlertController custom font, size, color, it seems that you can set it using KVO. The code for doing of so is similar:
var alertVC = UIAlertController(title: "Dont care what goes here, since we're about to change below", message: "", preferredStyle: .ActionSheet)
let hogan = NSMutableAttributedString(string: "Presenting the great... Hulk Hogan!")
hogan.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(50), range: NSMakeRange(24, 11))
alertVC.setValue(hogan, forKey: "attributedTitle")