I have a solution here. I created a custom AlertThemeConfigurator that recursively goes through all the subqueries that search for UILabels, and then sets the thematic Text attribute for them for the header, message, and various types of actions. Feel free to attribute attribute strings correctly.
class AlertThemeConfigurator { class func configureAlertViewController(alertController : UIAlertController) { AlertLabelConfigurator.adjustLabels(inView: alertController.view, alertController: alertController) } class AlertLabelConfigurator { class func adjustLabels(inView view : UIView, alertController : UIAlertController) { for subview in view.subviews { if subview is UILabel { adjustLabel((subview as! UILabel), inAlertViewController : alertController) } adjustLabels(inView :subview, alertController : alertController) } } class func adjustLabel(label : UILabel, inAlertViewController alertController : UIAlertController) { if label.text == alertController.title { label.attributedText = attributedTitle(label.text!) } else if label.text == alertController.message { label.attributedText = attributedBody(label.text!) } for action in alertController.actions { if label.text == action.title { switch action.style { case .Default: label.attributedText = attributedDefaultAction(action.title!) case .Cancel: label.attributedText = attributedCancelAction(action.title!) case .Destructive: label.attributedText = attributedDestructiveAction(action.title!) } } } } class func attributedTitle(title : String) -> NSAttributedString { let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 20)!, NSForegroundColorAttributeName : UIColor.greenColor()] return NSAttributedString(string: title, attributes: attributes) } class func attributedBody(title : String) -> NSAttributedString { let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 12)!, NSForegroundColorAttributeName : UIColor.orangeColor()] return NSAttributedString(string: title, attributes: attributes) } class func attributedDefaultAction(title : String) -> NSAttributedString { let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.yellowColor()] return NSAttributedString(string: title, attributes: attributes) } class func attributedCancelAction(title : String) -> NSAttributedString { let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.purpleColor()] return NSAttributedString(string: title, attributes: attributes) } class func attributedDestructiveAction(title : String) -> NSAttributedString { let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.redColor()] return NSAttributedString(string: title, attributes: attributes) } } }
To present a challenge
let alert = CustomAlertController(title: "Title", message:"Message" , preferredStyle: UIAlertControllerStyle.ActionSheet) alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil)) presentViewController(alert, animated: true, completion: nil) AlertThemeConfigurator.configureAlertViewController(alert)
AntonTheDev
source share