Hacking UIAlertController in Swift.
UIAlertController support iOS 8.0 and Later. In the previous tutorial of UIAlertController study about to define UIAlertController in Swift.
This tutorial provide the code about the customisation of UIAlertController in Swift. Before the customisation of UIAlertController, You have to Study about UIAlertController in Swift.
1. Change background Color of UIAlertController :
2. Change title Color and Font of UIAlertController :
3. Change Message Color and Font of UIAlertController :
4. Change Action button title color of UIAlertController :
Study UIAlertController tutorial and also more customization.
Thanks.
This tutorial provide the code about the customisation of UIAlertController in Swift. Before the customisation of UIAlertController, You have to Study about UIAlertController in Swift.
1. Change background Color of UIAlertController :
let alertController = UIAlertController(title: "Alert Title", message: "This is testing message.", preferredStyle: UIAlertControllerStyle.Alert)
// Background color.
let backView = alertController.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 10.0
backView?.backgroundColor = UIColor.yellowColor()
// Action.
let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
2. Change title Color and Font of UIAlertController :
let alertController = UIAlertController(title: "Alert Title", message: "This is testing message.", preferredStyle: UIAlertControllerStyle.Alert)
// Background color.
let backView = alertController.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 10.0
backView?.backgroundColor = UIColor.yellowColor()
// Change Title With Color and Font:
let myString = "Alert Title"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:0,length:myString.characters.count))
alertController.setValue(myMutableString, forKey: "attributedTitle")
// Action.
let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
3. Change Message Color and Font of UIAlertController :
let alertController = UIAlertController(title: "Alert Title", message: "This is testing message.", preferredStyle: UIAlertControllerStyle.Alert)
// Background color.
let backView = alertController.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 10.0
backView?.backgroundColor = UIColor.yellowColor()
// Change Title With Color and Font:
let myString = "Alert Title"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:0,length:myString.characters.count))
alertController.setValue(myMutableString, forKey: "attributedTitle")
// Change Message With Color and Font
let message = "This is testing message."
var messageMutableString = NSMutableAttributedString()
messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
messageMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:message.characters.count))
alertController.setValue(messageMutableString, forKey: "attributedMessage")
// Action.
let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
4. Change Action button title color of UIAlertController :
let alertController = UIAlertController(title: "Alert Title", message: "This is testing message.", preferredStyle: UIAlertControllerStyle.Alert)
// Background color.
let backView = alertController.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 10.0
backView?.backgroundColor = UIColor.yellowColor()
// Change Title With Color and Font:
let myString = "Alert Title"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:0,length:myString.characters.count))
alertController.setValue(myMutableString, forKey: "attributedTitle")
// Change Message With Color and Font
let message = "This is testing message."
var messageMutableString = NSMutableAttributedString()
messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
messageMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:message.characters.count))
alertController.setValue(messageMutableString, forKey: "attributedMessage")
// Action.
let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
action.setValue(UIColor.orangeColor(), forKey: "titleTextColor")
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
Study UIAlertController tutorial and also more customization.
Thanks.
Hacking UIAlertController in Swift.
Reviewed by KIRIT MODI
on
22:53:00
Rating:
No comments: