ios Swift 如何更改 UIAlertController 的标题颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31662591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift How to change UIAlertController's Title Color
提问by Jonathan Soifer
How do I change the UIAlertController's Title font using Swift?
如何使用 Swift 更改 UIAlertController 的标题字体?
I'm not talking about the message color, I'm not talking about the buttons color.
我不是在谈论消息颜色,我不是在谈论按钮颜色。
I'm talking about the Title.
我说的是标题。
回答by pbush25
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)
Added the correct line of code to my answer as it's much more concise than the answer below.
在我的答案中添加了正确的代码行,因为它比下面的答案简洁得多。
回答by Ignat
What push25 said is correct, only you have to use key-value coding in order to set the attributed string. (Thanks dupuis2387)
push25 说的是对的,只是你必须使用键值编码来设置属性字符串。(感谢dupuis2387)
//Define a color
let color = UIColor.redColor()
//Make a controller
let alertVC = UIAlertController(title: "Dont care what goes here, since we're about to change below", message: "", preferredStyle: UIAlertControllerStyle.Alert)
//Title String
var hogan = NSMutableAttributedString(string: "Presenting the great... Hulk Hogan!")
//Make the attributes, like size and color
hogan.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(40.0), range: NSMakeRange(24, 11))
hogan.addAttribute(NSForegroundColorAttributeName, value: color, range: NSMakeRange(0, NSString(string: hogan.string).length))
//Set the new title
//Use "attributedMessage" for the message
alertVC.setValue(hogan, forKey: "attributedTitle")
//This will change the button color
alertVC.view.tintColor = UIColor.orangeColor()
//Make the button
let button:UIAlertAction = UIAlertAction(title: "Label text", style: UIAlertActionStyle.Default, handler: { (e:UIAlertAction!) -> Void in
println("\(e)")
})
//You can add images to the button
let accessoryImage:UIImage = UIImage(named: "someImage")!
button.setValue(accessoryImage, forKey:"image")
//Add the button to the alert
alertVC.addAction(button)
//Finally present it
self.presentViewController(alertVC, animated: true, completion: nil)
回答by user2643679
Alert(self, Title: “Hello”, TitleColor: UIColor.whiteColor(), Message: “World”, MessageColor: UIColor.whiteColor(), BackgroundColor: UIColor.blackColor(), BorderColor: UIColor.yellowColor(), ButtonColor: UIColor.yellowColor())
func Alert(View: ViewController, Title: String, TitleColor: UIColor, Message: String, MessageColor: UIColor, BackgroundColor: UIColor, BorderColor: UIColor, ButtonColor: UIColor) {
let TitleString = NSAttributedString(string: Title, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(15), NSForegroundColorAttributeName : TitleColor])
let MessageString = NSAttributedString(string: Message, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(15), NSForegroundColorAttributeName : MessageColor])
let alertController = UIAlertController(title: Title, message: Message, preferredStyle: .Alert)
alertController.setValue(TitleString, forKey: "attributedTitle")
alertController.setValue(MessageString, forKey: "attributedMessage")
let okAction = UIAlertAction(title: "OK", style: .Default) { (action) in
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)
let subview = alertController.view.subviews.first! as UIView
let alertContentView = subview.subviews.first! as UIView
alertContentView.backgroundColor = BackgroundColor
alertContentView.layer.cornerRadius = 10
alertContentView.alpha = 1
alertContentView.layer.borderWidth = 1
alertContentView.layer.borderColor = BorderColor.CGColor
//alertContentView.tintColor = UIColor.whiteColor()
alertController.view.tintColor = ButtonColor
View.presentViewController(alertController, animated: true) {
// ...
}
}
回答by Fabio
I construct this class:
我构造这个类:
class Alert {
class func showAlert(title: String, titleColor: UIColor, message: String, preferredStyle: UIAlertControllerStyle, titleAction: String, actionStyle: UIAlertActionStyle, vc: UIViewController) {
let attributedString = NSAttributedString(string: title, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16), NSAttributedStringKey.foregroundColor: titleColor])
let alert = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
alert.setValue(attributedString, forKey: "attributedTitle")
alert.addAction(UIAlertAction(title: titleAction, style: actionStyle, handler: nil))
vc.present(alert, animated: true)
}
}
}
Usage
用法
Alert.showAlert(title: "yourTitle", titleColor: .yourcolor, message: "your message", preferredStyle: .alert, titleAction: "your title action", actionStyle: .yourActionStyle, vc: self)
Hope this help :)
希望这有帮助:)
回答by MrG
Here is for Swift 4 ++ also using a global instance for UIAlert
这是 Swift 4 ++ 也使用 UIAlert 的全局实例
func showAlertError(withTitle title: String, withMessage message: String) {
var dialogTitle = title
if dialogTitle.isEmpty { dialogTitle = "Error:" }
let attributedString = NSAttributedString(string: dialogTitle, attributes: [
NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15),
NSAttributedString.Key.foregroundColor : UIColor.red
])
let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
alert.setValue(attributedString, forKey: "attributedTitle")
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(ok)
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
}
回答by xc000
Also you can add a UIAlertAction with title " "(space) and add a custom UILabel to UIAlertController.view
at the place of title.
您也可以添加标题为“”(空格)的 UIAlertAction 并在标题位置添加自定义 UILabel UIAlertController.view
。