let dictTitleColor = [NSAttributedStringKey.foregroundColor : UIColor.green,
NSAttributedStringKey.font : UIFont.preferredFont(forTextStyle: .headline)]
let titleAttributedString = NSMutableAttributedString(string: title, attributes: dictTitleColor)
alert.setValue(titleAttributedString, forKey: "attributedTitle")
NOTE:如果出现弹出窗口,然后用户在辅助功能设置中更改了字体大小,则此操作将会失败。对于这种情况,您可能需要听UIContentSizeCategory.didChangeNotification https://developer.apple.com/documentation/uikit/uicontentsizecategory/1622948-didchangenotification并在那里更新字体大小。
e.g.
NotificationCenter.default.addObserver(self, selector: #selector(preferredContentSizeChanged(_:)), name: UIContentSizeCategory.didChangeNotification, object: nil)
方法
@objc func preferredContentSizeChanged(_ notification: Notification) {
let dictTitleColor = [NSAttributedStringKey.foregroundColor : UIColor.green,
NSAttributedStringKey.font : UIFont.preferredFont(forTextStyle: .headline)]
let titleAttributedString = NSMutableAttributedString(string: title, attributes: dictTitleColor)
alert.setValue(titleAttributedString, forKey: "attributedTitle")
}