UIAlertController change font color

后端 未结 8 755
时光取名叫无心
时光取名叫无心 2021-02-05 06:43

Here\'s my code that creates the UIAlertController

    // Create the alert controller
    var alertController = UIAlertController(title: \"Are you sure you want          


        
相关标签:
8条回答
  • 2021-02-05 07:23

    Here's an update for Swift 4, using Cody's answer as a base:

    Setting a colour for the alert title:

    alert.setValue(NSAttributedString(string: alert.title!, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.medium), NSAttributedStringKey.foregroundColor : UIColor.blue]), forKey: "attributedTitle")
    

    Setting a colour for the alert message:

    alert.setValue(NSAttributedString(string: alert.message, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.Medium), NSAttributedStringKey.foregroundColor : UIColor.green]), forKey: "attributedMessage")
    

    As per https://developer.apple.com/documentation/foundation/nsattributedstring/key

    0 讨论(0)
  • 2021-02-05 07:27

    Below code is changing the UIAlertView title color.

      let alert = UIAlertController(title: messageTitle, message: messageText, preferredStyle: UIAlertControllerStyle.Alert)
    
      alert.setValue(NSAttributedString(string: messageTitle, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(17),NSForegroundColorAttributeName : UIColor.redColor()]), forKey: "attributedTitle")
    
      alert.addAction(UIAlertAction(title: buttonText, style: UIAlertActionStyle.Default, handler: nil))
    
      parent.presentViewController(alert, animated: true, completion: nil)
    

    If you want to change button color then add following code after present View Controller.

      alert.view.tintColor = UIColor.redColor()
    
    0 讨论(0)
  • 2021-02-05 07:27

    In Swift 5 and XCode 11.1 and later, colour for the alert title:

    alert.setValue(NSAttributedString(string: alert.title!, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 25, weight: UIFont.Weight.medium), NSAttributedString.Key.foregroundColor : UIColor.red]), forKey: "attributedTitle")
    

    Colour for the alert message:

    alert.setValue(NSAttributedString(string: alert.message!, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20,weight: UIFont.Weight.medium),NSAttributedString.Key.foregroundColor :UIColor.black]), forKey: "attributedMessage")
    
    0 讨论(0)
  • 2021-02-05 07:27

    Before iOS 9.0 you could simply change the underlying view's tintColor like this:

    alertController.view.tintColor = UIColor.redColor()
    

    However, due to a bug introduced in iOS 9, you can either:

    1. Change the app tintColor in the AppDelegate.

      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
          self.window.tintColor = UIColor.redColor()
          return true
      }
      
    2. Reapply the color in the completion block.

      self.presentViewController(alert, animated: true, completion: {() -> Void in
          alert.tintColor = UIColor.redColor()
      })
      

    See my other answer here: https://stackoverflow.com/a/37737212/1781087

    0 讨论(0)
  • 2021-02-05 07:29

    Piyush's answer helped me the most, but here are some tweaks for Swift 3 and to change the title and message separately.

    Title:

    alert.setValue(NSAttributedString(string: alert.message, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 29, weight: UIFontWeightMedium), NSForegroundColorAttributeName : UIColor.red]), forKey: "attributedTitle")
    

    Message:

    alert.setValue(NSAttributedString(string: alert.message, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 29, weight: UIFontWeightMedium), NSForegroundColorAttributeName : UIColor.red]), forKey: "attributedMessage")
    

    The big font size is because I actually needed to do it for tvOS, works great on it and iOS.

    0 讨论(0)
  • 2021-02-05 07:34

    After some trial and error, I found this worked. Hopefully this will help future swift newcomers!

    alertController.view.tintColor = UIColor.blackColor()
    
    0 讨论(0)
提交回复
热议问题