4

I am new to ios and developing my first application with swift. This app contains many popups during play with app. And i want to make this very attractive.

In-built UIAlertcontroller is spoil my look and feel. So is there any way to fully customize UIAlertController.

I want to change FONT AND COLOR of TITLE AND MESSAGE and also BACKGROUND COLOR and Buttons too.

Thanks.

dfrib
  • 70,367
  • 12
  • 127
  • 192

3 Answers3

10

your question seems duplicate but not duplicate.

Yes it is possible. You can refer this code.

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)

Output :

enter image description here

Reference link : Customise UIAlertController

Chetan Prajapati
  • 2,249
  • 19
  • 24
  • 7
    from the [docs](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/): *The UIAlertController class is intended to be used as-is […]. The view hierarchy for this class is private and must not be modified.* So the app might get rejected or not. – vikingosegundo May 27 '16 at 10:29
  • 1
    AND? "attributedTitle" is a private API and nobody will pass your app into app store – Vyachaslav Gerchicov Nov 25 '16 at 08:18
1

Same as @Chetan Prajapati but in order to make the code clear and easy to use, consider this custom alert class that I made. You can now use this class just as with normal UIAlertController with you own customization:

class AlertDialogController: UIAlertController {
    public convenience init(title: String?, message: String?){
        self.init(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        
        // Background color.
        let backView = view.subviews.last?.subviews.last
        backView?.layer.cornerRadius = 16.0
        backView?.backgroundColor = UIColor.white
        backView?.layer.borderWidth = 1
        backView?.layer.masksToBounds = true
        backView?.layer.borderColor = UIColor.red.cgColor
        
        //Title Color
        if let title = title {
            var myMutableString = NSMutableAttributedString()
            myMutableString = NSMutableAttributedString(string: title as String, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 17.0)])
            myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange(location:0, length:title.count))
            self.setValue(myMutableString, forKey: "attributedTitle")
        }
        
        //Message Color
        if let message = message {
            var messageMutableString = NSMutableAttributedString()
            messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 17.0)])
            messageMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location:0, length:message.count))
            self.setValue(messageMutableString, forKey: "attributedMessage")
        }
    }
    
    public func addAction(_ action: UIAlertAction, titleColor: UIColor) {
        //Action button color
        action.setValue(titleColor, forKey: "titleTextColor")
        super.addAction(action)
    }
}
0

I modified the code for Swift5 users

What you can achieve through the below code?

  1. you can set the background color of the message (it has 2 options.)

    Option2 you need to extend uialertcontroller in your class. ( for me, option 2 works very well (i am setting it as black color).
    option 1 looks like a white color layer is on top of the background color.)

  2. change string of button (cancel & confirm ) and color.

enter image description here

let alert = customDialog(title: "title test ", message: "message test")
self.present(alert, animated: true)

    func customDialog(title:String, message : String) -> UIAlertController {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)

//Background color option1.
            let backView = alertController.view.subviews.last?.subviews.last
            backView?.layer.cornerRadius = 10.0
        backView?.backgroundColor = UIColor.yellow
   
//Background option2. (u need to extend uialertcontroller )
/*
alertController.setBackgroundColor(color: UIColor.yellow)

*/
 
            // Change Title With Color and Font:
    
            var myMutableString = NSMutableAttributedString()
        myMutableString = NSMutableAttributedString(string: title as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: 18.0)!])
        myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:0,length:title.count))
            alertController.setValue(myMutableString, forKey: "attributedTitle")
    
            // Change Message With Color and Font
    
            var messageMutableString = NSMutableAttributedString()
        messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: 18.0)!])
        messageMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange(location:0,length:message.count))
            alertController.setValue(messageMutableString, forKey: "attributedMessage")
    
    
            // Action.
        let okAction = UIAlertAction(title: "Confirm", style: .default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        okAction.setValue(UIColor.orange, forKey: "titleTextColor") //ok in orange color
    
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        
    
       // self.present(alertController, animated: true, completion: nil)
        
        return alertController
    }



if u select option2. background . please extent below https://www.swiftdevcenter.com/change-font-text-color-and-background-color-of-uialertcontroller/

extension UIAlertController {

    //Set background color of UIAlertController
    func setBackgroundColor(color: UIColor) {
        if let bgView = self.view.subviews.first, let groupView = bgView.subviews.first, let contentView = groupView.subviews.first {
            contentView.backgroundColor = color
        }
    }

}


Energy
  • 940
  • 13
  • 20