uialertaction _titletextcolor

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

uialertaction _titletextcolor
UIAlertAction 是 iOS 开发中用于创建对话框动作的类。

通过 UIAlertAction,你可以为对话框添加多个操作,例如“确定”、“取消”等。

但是,UIAlertAction 类本身并没有提供直接设置标题文本颜色的方法。

要在 UIAlertAction 中设置标题文本颜色,你需要使用 UIAppearance API 或者通过自定义 UIAlertController 来实现。

使用 UIAppearance API 的方法如下:
swift
UIAlertAction.appearance().setTitleTextAttributes([NSAttributedString.Key.f oregroundColor: UIColor.red], for: .normal)
上述代码将所有 UIAlertAction 的标题文本颜色设置为红色。

你可以根据需要修改颜色值。

如果你想为特定的UIAlertAction 设置标题文本颜色,你需要自定义UIAlertController:
创建一个继承自 UIAlertController 的类。

在该类中重写 preferredContentSize 方法,设置内容大小为默认大小。

在该类中重写 viewDidLayoutSubviews 方法,在视图布局完成后执行自定义操作。

在 viewDidLayoutSubviews 方法中,获取 UIAlertController 的 alertView,并遍历其子视图,找到 title view,然后设置其 textColor 属性。

以下是一个示例代码:
swift
class CustomAlertController: UIAlertController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let titleView = self.view.subviews.first?.subviews.first?.subviews.first as? UILabel else { return }
titleView.textColor = UIColor.red // 设置标题文本颜色为红色
}
}
在创建 UIAlertAction 时,使用自定义的 UIAlertController:
swift
let alert = CustomAlertController()
let action = UIAlertAction(title: "确定", style: .default) { action in // 执行操作
}
alert.addAction(action)
present(alert, animated: true, completion: nil)。

相关文档
最新文档