ios - Swift 如何更改 UIAlertController 的标题颜色

标签 ios xcode swift user-interface

如何使用 Swift 更改 UIAlertController 的标题字体?

我不是在谈论消息颜色,我不是在谈论按钮颜色。

我说的是标题。

最佳答案

let attributedString = NSAttributedString(string: "Title", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here
    NSForegroundColorAttributeName : UIColor.redColor()
])

let alert = UIAlertController(title: "", message: "",  preferredStyle: .alert)

alert.setValue(attributedString, forKey: "attributedTitle")

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
}

alert.addAction(cancelAction)

present(alert, animated: true, completion: nil)

在我的答案中添加了正确的代码行,因为它比下面的答案简洁得多。

关于ios - Swift 如何更改 UIAlertController 的标题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31662591/

相关文章:

ios - 将参数传递给 addTarget :action:forControlEvents

swift - 我可以将参数传递给 Swift 中的 Singleton 类吗?和一般单例使用

ios - Xcode7 - 等高乘数约束导致约束冲突

ios - 有没有在viewDidLayoutSubviews之后调用的方法?

iphone - 我可以阻止按钮吗?

objective-c - iOS - 删除成就

ios - iOS 6 上无法获取ABAddressBook(无隐私提示返回空数据库)

xcode - "Archive"构建中是否启用了 Xcode 断点?

objective-c - 如何在 XCode 中编译和运行 Objective-C 项目?

ios - 如何删除 Xcode 警告 Apple Mach-O Linker Warning 'Pointer not aligned at address