如何为UIAlertController设置自定义字体(含按钮字体)
UIAlertController 按钮自定义字体解决方案
你之前尝试给UIAlertController设置titleTextFont的方式无效,因为这个键不属于UIAlertController,按钮的文字样式由UIAlertAction控制,下面是正确的实现方式:
核心修正点
移除无效的alertController.setValue(attributedOk, forKey: "titleTextFont")代码,转而给UIAlertAction设置富文本标题。
完整兼容代码(iOS 13+)
let alertController = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(newErrorMessage, comment: "") , preferredStyle: .alert) alertController.view.tintColor = UIConfiguration.DarkGreen! // 配置标题自定义字体 let titleFont = LanguageManager.boldFont(size: 17.0) let attributedTitle = NSAttributedString(string: NSLocalizedString(title, comment: ""), attributes: [NSAttributedString.Key.font: titleFont]) alertController.setValue(attributedTitle, forKey: "attributedTitle") // 配置消息文本自定义字体 let messageFont = LanguageManager.regularFont(size: 12.0) let attributedMessage = NSAttributedString(string: NSLocalizedString(newErrorMessage, comment: ""), attributes: [NSAttributedString.Key.font: messageFont]) alertController.setValue(attributedMessage, forKey: "attributedMessage") // 配置OK按钮自定义字体 let okTitle = NSLocalizedString("Ok", comment: "") let okFont = LanguageManager.boldFont(size: 16.0) let attributedOk = NSAttributedString(string: okTitle, attributes: [ NSAttributedString.Key.font: okFont, NSAttributedString.Key.foregroundColor: UIConfiguration.DarkGreen! // 可选:统一按钮文字颜色与弹窗tintColor ]) let okAction = UIAlertAction(title: okTitle, style: .default, handler: nil) okAction.setValue(attributedOk, forKey: "attributedTitle") alertController.addAction(okAction) present(alertController, animated: true, completion: nil)
iOS 15+ 官方API方案
iOS 15及以上,Apple提供了无需KVC的官方配置方式,更稳定安全:
let alertController = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(newErrorMessage, comment: "") , preferredStyle: .alert) alertController.view.tintColor = UIConfiguration.DarkGreen! // 标题和消息配置同上,略去重复代码... let okTitle = NSLocalizedString("Ok", comment: "") let okFont = LanguageManager.boldFont(size: 16.0) if #available(iOS 15.0, *) { var actionConfig = UIAlertAction.Configuration() actionConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incomingAttrs in var transformedAttrs = incomingAttrs transformedAttrs.font = okFont transformedAttrs.foregroundColor = UIConfiguration.DarkGreen! return transformedAttrs } let okAction = UIAlertAction(title: okTitle, style: .default, handler: nil, configuration: actionConfig) alertController.addAction(okAction) } else { // 回退到KVC方案兼容低版本 let attributedOk = NSAttributedString(string: okTitle, attributes: [ NSAttributedString.Key.font: okFont, NSAttributedString.Key.foregroundColor: UIConfiguration.DarkGreen! ]) let okAction = UIAlertAction(title: okTitle, style: .default, handler: nil) okAction.setValue(attributedOk, forKey: "attributedTitle") alertController.addAction(okAction) } present(alertController, animated: true, completion: nil)
注意事项
- 低版本iOS使用KVC设置
attributedTitle属于访问私有属性,但苹果审核通常不会拒绝这种常规自定义需求; - iOS 15+优先使用官方API,避免私有属性带来的潜在适配问题。
内容的提问来源于stack exchange,提问作者Nicolas
相关产品推荐
相关产品推荐

