UIAlertController custom font, size, color

后端 未结 25 1643
遥遥无期
遥遥无期 2020-11-22 09:06

I am using new UIAlertController for showing alerts. I have this code:

// nil titles break alert interface on iOS 8.0, so we\'ll be using empty strings
UIAle         


        
相关标签:
25条回答
  • 2020-11-22 09:49

    I just completed a replacement for UIAlertController. This is the only sensible way to go, I think:


    Old

    Here's my method in Swift which mashes up a lot of information from answers here

    func changeAlert(alert: UIAlertController, backgroundColor: UIColor, textColor: UIColor, buttonColor: UIColor?) {
        let view = alert.view.firstSubview().firstSubview()
        view.backgroundColor = backgroundColor
        view.layer.cornerRadius = 10.0
    
        // set color to UILabel font
        setSubviewLabelsToTextColor(textColor, view: view)
    
        // set font to alert via KVC, otherwise it'll get overwritten
        let titleAttributed = NSMutableAttributedString(
            string: alert.title!,
            attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(17)])
        alert.setValue(titleAttributed, forKey: "attributedTitle")
    
    
        let messageAttributed = NSMutableAttributedString(
            string: alert.message!,
            attributes: [NSFontAttributeName:UIFont.systemFontOfSize(13)])
        alert.setValue(messageAttributed, forKey: "attributedMessage")
    
    
        // set the buttons to non-blue, if we have buttons
        if let buttonColor = buttonColor {
            alert.view.tintColor = buttonColor
        }
    }
    
    func setSubviewLabelsToTextColor(textColor: UIColor, view:UIView) {
        for subview in view.subviews {
            if let label = subview as? UILabel {
                label.textColor = textColor
            } else {
                setSubviewLabelsToTextColor(textColor, view: subview)
            }
        }
    }
    

    This works in some situations perfectly, and in others it's a total fail (the tint colors do not show as expected).

    0 讨论(0)
  • 2020-11-22 09:49

    You can use the following syntax without any private API.

    alert.view.tintColor = alert.view.tintColor = UIColor.green
    alert.setTitle(font: UIFont.boldSystemFont(ofSize: 26), color: UIColor.darkGray)
    alert.setMessage(font: UIFont.systemFont(ofSize: 26), color: UIColor.darkGray)
    

    0 讨论(0)
  • In Xcode 8 Swift 3.0

    @IBAction func touchUpInside(_ sender: UIButton) {
    
        let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
    
        //to change font of title and message.
        let titleFont = [NSFontAttributeName: UIFont(name: "ArialHebrew-Bold", size: 18.0)!]
        let messageFont = [NSFontAttributeName: UIFont(name: "Avenir-Roman", size: 12.0)!]
    
        let titleAttrString = NSMutableAttributedString(string: "Title Here", attributes: titleFont)
        let messageAttrString = NSMutableAttributedString(string: "Message Here", attributes: messageFont)
    
        alertController.setValue(titleAttrString, forKey: "attributedTitle")
        alertController.setValue(messageAttrString, forKey: "attributedMessage")
    
        let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
            print("\(action.title)")
        }
    
        let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
            print("\(action.title)")
        }
    
        let action3 = UIAlertAction(title: "Action 3", style: .default) { (action) in
            print("\(action.title)")
        }
    
        let okAction = UIAlertAction(title: "Ok", style: .default) { (action) in
            print("\(action.title)")
        }
    
        alertController.addAction(action1)
        alertController.addAction(action2)
        alertController.addAction(action3)
        alertController.addAction(okAction)
    
        alertController.view.tintColor = UIColor.blue
        alertController.view.backgroundColor = UIColor.black
        alertController.view.layer.cornerRadius = 40
    
        present(alertController, animated: true, completion: nil)
    
    }
    

    Output

    0 讨论(0)
  • 2020-11-22 09:50

    Here is an extension for Swift 4.1 and Xcode 9.4.1:

    extension UIAlertController{
    
    func addColorInTitleAndMessage(color:UIColor,titleFontSize:CGFloat = 18, messageFontSize:CGFloat = 13){
    
        let attributesTitle = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: titleFontSize)]
        let attributesMessage = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: UIFont.systemFont(ofSize: messageFontSize)]
        let attributedTitleText = NSAttributedString(string: self.title ?? "", attributes: attributesTitle)
        let attributedMessageText = NSAttributedString(string: self.message ?? "", attributes: attributesMessage)
    
        self.setValue(attributedTitleText, forKey: "attributedTitle")
        self.setValue(attributedMessageText, forKey: "attributedMessage")
    
    }}
    
    0 讨论(0)
  • 2020-11-22 09:54

    You can use an external library like PMAlertController without using workaround, where you can substitute Apple's uncustomizable UIAlertController with a super customizable alert.

    Compatible with Xcode 8, Swift 3 and Objective-C


    Features:

    • [x] Header View
    • [x] Header Image (Optional)
    • [x] Title
    • [x] Description message
    • [x] Customizations: fonts, colors, dimensions & more
    • [x] 1, 2 buttons (horizontally) or 3+ buttons (vertically)
    • [x] Closure when a button is pressed
    • [x] Text Fields support
    • [x] Similar implementation to UIAlertController
    • [x] Cocoapods
    • [x] Carthage
    • [x] Animation with UIKit Dynamics
    • [x] Objective-C compatibility
    • [x] Swift 2.3 & Swift 3 support
    0 讨论(0)
  • 2020-11-22 09:54

    In Swift 4.1 and Xcode 10

    //Displaying alert with multiple actions and custom font ans size
    let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
    
    let titFont = [NSAttributedStringKey.font: UIFont(name: "ArialHebrew-Bold", size: 15.0)!]
    let msgFont = [NSAttributedStringKey.font: UIFont(name: "Avenir-Roman", size: 13.0)!]
    
    let titAttrString = NSMutableAttributedString(string: "Title Here", attributes: titFont)
    let msgAttrString = NSMutableAttributedString(string: "Message Here", attributes: msgFont)
    
    alert.setValue(titAttrString, forKey: "attributedTitle")
    alert.setValue(msgAttrString, forKey: "attributedMessage")
    
    let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
        print("\(String(describing: action.title))")
    }
    
    let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
        print("\(String(describing: action.title))")
    }
    
    let okAction = UIAlertAction(title: "Ok", style: .default) { (action) in
        print("\(String(describing: action.title))")
    }
    alert.addAction(action1)
    alert.addAction(action2)
    alert.addAction(okAction)
    
    alert.view.tintColor = UIColor.blue
    alert.view.layer.cornerRadius = 40
    // //If required background colour 
    // alert.view.backgroundColor = UIColor.white
    
    DispatchQueue.main.async(execute: {
        self.present(alert, animated: true)
    })
    
    0 讨论(0)
提交回复
热议问题