swift - 在 Swift 中更改消息 UIAlertController 的颜色

标签 swift uicolor uialertcontroller

我想快速更改 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 - 在 Swift 中更改消息 UIAlertController 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31296457/

相关文章:

swift - 接受 CGFloat 的 '\*=' 的重载

iphone - 使用 UIGraphicsPushContext(context) 和 UIGraphicsPopContext()

swift - 如何使警报只出现一次

ios - 使用 Swift 的 iOS 代码结构,所有代码放在哪里?

swift - 发出 Base64 解码特定字符串

swift - 在 Linux 上使用 Swift 编程

xcode - Interface Builder 的颜色选择器中的颜色错误

iphone - UITableViewCell textColor 不会因 userInteractionEnabled = NO 而改变

ios - 如何同时呈现 UIMenuController 和 UIAlertController?

ios - 当不在 View Controller 中时,如何显示UIAlertController?