Swift Comment changer la couleur du titre de UIAlertController

Comment changer la police du titre de UIAlertController en utilisant Swift?

Je ne parle pas de la couleur du message, je ne parle pas de la couleur des buttons.

Je parle du titre.

let atsortingbutedSsortingng = NSAtsortingbutedSsortingng(ssortingng: "Title", atsortingbutes: [ NSFontAtsortingbuteName : UIFont.systemFontOfSize(15), //your font here NSForegroundColorAtsortingbuteName : UIColor.redColor() ]) let alert = UIAlertController(title: "", message: "", preferredStyle: .Alert) alert.setValue(atsortingbutedSsortingng, forKey: "atsortingbutedTitle") let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction!) -> Void in } presentViewController(alert, animated: true, completion: nil) 

J'ai ajouté la bonne ligne de code à ma réponse car elle est beaucoup plus concise que la réponse ci-dessous.

Ce que push25 dit est correct, seulement vous devez utiliser le encoding à key-valeur afin de définir la string atsortingbuée. (Merci 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 Ssortingng var hogan = NSMutableAtsortingbutedSsortingng(ssortingng: "Presenting the great... Hulk Hogan!") //Make the atsortingbutes, like size and color hogan.addAtsortingbute(NSFontAtsortingbuteName, value: UIFont.systemFontOfSize(40.0), range: NSMakeRange(24, 11)) hogan.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: color, range: NSMakeRange(0, NSSsortingng(ssortingng: hogan.ssortingng).length)) //Set the new title //Use "atsortingbutedMessage" for the message alertVC.setValue(hogan, forKey: "atsortingbutedTitle") //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) 
 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: Ssortingng, TitleColor: UIColor, Message: Ssortingng, MessageColor: UIColor, BackgroundColor: UIColor, BorderColor: UIColor, ButtonColor: UIColor) { let TitleSsortingng = NSAtsortingbutedSsortingng(ssortingng: Title, atsortingbutes: [NSFontAtsortingbuteName : UIFont.systemFontOfSize(15), NSForegroundColorAtsortingbuteName : TitleColor]) let MessageSsortingng = NSAtsortingbutedSsortingng(ssortingng: Message, atsortingbutes: [NSFontAtsortingbuteName : UIFont.systemFontOfSize(15), NSForegroundColorAtsortingbuteName : MessageColor]) let alertController = UIAlertController(title: Title, message: Message, preferredStyle: .Alert) alertController.setValue(TitleSsortingng, forKey: "atsortingbutedTitle") alertController.setValue(MessageSsortingng, forKey: "atsortingbutedMessage") 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) { // ... } } 

Vous pouvez également append un UIAlertAction avec le titre "" (espace) et append un UILabel personnalisé à UIAlertController.view à l'endroit du titre.