Swift How to change UIAlertController's Title Color

后端 未结 6 1499
鱼传尺愫
鱼传尺愫 2020-12-14 09:27

How do I change the UIAlertController\'s Title font using Swift?

I\'m not talking about the message color, I\'m not talking about the buttons color.

I\'m tal

6条回答
  •  感情败类
    2020-12-14 09:59

    What push25 said is correct, only you have to use key-value coding in order to set the attributed string. (Thanks 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)
    

提交回复
热议问题