在 Swift 中更改消息 UIAlertController 的颜色
Change the color of the message UIAlertController in Swift
我想在 swift 中更改 UIAlertController 中消息的颜色。默认是黑色我想把它改成红色
我的 UIAlertController 如下所示:
alert = UIAlertController(title: "", message: "Invalid Name", preferredStyle: UIAlertControllerStyle.Alert)
alert.view.tintColor = UIColor.blackColor()
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
}))
self.presentViewController(alert, animated: true, completion: {
println("completion block")
})
我想更改上面警告框中无效名称的颜色。
您可以使用attributedStrings
创建您想要的颜色、字体、大小和样式,然后将字符串设置为标题。
编辑:更新了示例
let attributedString = NSAttributedString(string: "Invalid Name", attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.systemFontOfSize(15),
NSForegroundColorAttributeName : UIColor.redColor()
])
let alert = UIAlertController(title: "Title", message: "", preferredStyle: .Alert)
alert.setValue(attributedString, forKey: "attributedMessage")
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
presentViewController(alert,
animated: true,
completion: nil)
Swift 5
let messageString = "The message to display"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: messageString as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Poppins-medium", size: 14.0)!])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location:0,length:messageString.count))
alert.setValue(myMutableString, forKey: "attributedMessage")
我想在 swift 中更改 UIAlertController 中消息的颜色。默认是黑色我想把它改成红色
我的 UIAlertController 如下所示:
alert = UIAlertController(title: "", message: "Invalid Name", preferredStyle: UIAlertControllerStyle.Alert)
alert.view.tintColor = UIColor.blackColor()
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
}))
self.presentViewController(alert, animated: true, completion: {
println("completion block")
})
我想更改上面警告框中无效名称的颜色。
您可以使用attributedStrings
创建您想要的颜色、字体、大小和样式,然后将字符串设置为标题。
编辑:更新了示例
let attributedString = NSAttributedString(string: "Invalid Name", attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.systemFontOfSize(15),
NSForegroundColorAttributeName : UIColor.redColor()
])
let alert = UIAlertController(title: "Title", message: "", preferredStyle: .Alert)
alert.setValue(attributedString, forKey: "attributedMessage")
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
presentViewController(alert,
animated: true,
completion: nil)
Swift 5
let messageString = "The message to display"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: messageString as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Poppins-medium", size: 14.0)!])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location:0,length:messageString.count))
alert.setValue(myMutableString, forKey: "attributedMessage")