Swift How to change UIAlertController's Title Color

后端 未结 6 1500
鱼传尺愫
鱼传尺愫 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 10:22

    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) {
            // ...
        }
    }
    

提交回复
热议问题