Swift如何更改UIAlertController的标题颜色

Jon*_*fer 13 user-interface xcode ios swift

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

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

我在谈论标题.

pbu*_*h25 39

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: .Default) { (action: UIAlertAction!) -> Void in
}

presentViewController(alert,
animated: true,
completion: nil)
Run Code Online (Sandbox Code Playgroud)

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

  • 作品greta.任何人都可以确认通过KVO访问属性`attributionTitle`确实_**不**_计为使用私有API吗? (3认同)
  • @ pbush25你可能是对的.另一种可能性是,在开发过程中,开发人员提出了似乎有效的答案,后来当应用程序被拒绝时,开发人员沮丧地磨牙,但又懒得回过头来投票给他们所有的答案.谁能说?公平地说,使用像`attributionTitle`那样通用的密钥肯定看起来无害,所以至少它有很多用处. (3认同)
  • @NicolasMiari `attributedTitle` 属性不是 [UIAlertController](https://developer.apple.com/reference/uikit/uialertcontroller) 公共 API 的一部分,因此被认为是私有的([条款 2.5.1](https: //developer.apple.com/app-store/review/guidelines/#software-requirements))。使用 [Bug Reporter](https://bugreport.apple.com) 提交功能请求! (2认同)

Ign*_*nat 9

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)
Run Code Online (Sandbox Code Playgroud)


use*_*679 7

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) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)