Is it possible to set custom font & size for NSString?

后端 未结 1 1165
孤街浪徒
孤街浪徒 2021-01-17 03:39

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

相关标签:
1条回答
  • 2021-01-17 04:12

    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")
    
    0 讨论(0)
提交回复
热议问题