How to edit UIAlertAction text font size and color

后端 未结 7 896
时光取名叫无心
时光取名叫无心 2020-12-06 01:33

How to edit UIAlertAction text size and color? I have taken a UIAlertController acoording to it how to edit the size. This i smy Code



        
7条回答
  •  粉色の甜心
    2020-12-06 01:59

    Here is giving my answer Swift 5. We can apply custom fonts to AlertView and AlertAddAction.
    1st Create AlertView Extention. Inside Extension

    import Foundation
    import UIKit
    
    extension UIAlertController {
        
        func applyBranding() {
            
            applyAlertTitleBranding()
            applyAlertMessageBranding()
        }
        
        func applyAlertTitleBranding() {
            let titleFont = [kCTFontAttributeName: UIFont(name: "Montserrat-Medium", size: 18.0)!]
            let titleAttrString = NSMutableAttributedString(string: title!, attributes: titleFont as [NSAttributedString.Key : Any])
            let titleColor = UIColor(red: 34.0/255/0, green: 34.0/255/0, blue: 34.0/255/0, alpha: 1.0)
            titleAttrString.addAttribute(NSAttributedString.Key.foregroundColor,
                                         value: titleColor,
                                         range: NSRange(location: 0, length: title!.count))
            setValue(titleAttrString, forKey: "attributedTitle")
        }
        
        func applyAlertMessageBranding() {
            let messageFont = [kCTFontAttributeName: UIFont(name: "Montserrat-Regular", size: 14.0)!]
            let messageAttrString = NSMutableAttributedString(string: message!, attributes: messageFont as [NSAttributedString.Key : Any])
            let messageTitleColor = UIColor(red: 68.0/255/0, green: 68.0/255/0, blue: 68.0/255/0, alpha: 1.0)
            messageAttrString.addAttribute(NSAttributedString.Key.foregroundColor,
                                           value: messageTitleColor,
                                           range: NSRange(location: 0, length: message!.count))
            setValue(messageAttrString, forKey: "attributedMessage")
        }
        
        func applyNoActionBranding() {
            let font = [kCTFontAttributeName: UIFont(name: "Montserrat-Medium", size: 16.0)!]
            for actionButton in actions {
                let titleAttrString = NSMutableAttributedString(string: actionButton.title!, attributes: font as [NSAttributedString.Key : Any])
                actionButton.setValue(titleAttrString, forKey: "attributedTitleForAction")
            }
        }
        
        func applyYesActionBranding() {
            let font = [kCTFontAttributeName: UIFont(name: "Montserrat-Regular", size: 16.0)!]
            for actionButton in actions {
                let titleAttrString = NSMutableAttributedString(string: actionButton.title!, attributes: font as [NSAttributedString.Key : Any])
                actionButton.setValue(titleAttrString, forKey: "attributedTitleForAction")
            }
        }
    }
    
    
    
     
    

    2nd call those AlertView functions where ever you need. For example call in ViewDidLoad()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        showAlert()
    }
    
    func showAlert() {
    
        let alertVc = UIAlertController(title: "Some Title", message: "Some message", preferredStyle: .alert)
        
        //No action
        let noAction = UIAlertAction(title: "No", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            alertVc.dismiss(animated: true, completion: nil)
        })
        
        //Yes action
        let yesAction = UIAlertAction(title: "Yes", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            self.deleteFunction(address)
            alertVc.dismiss(animated: true, completion: nil)
        })
        
        alertVc.applyBranding()
        alertVc.applyNoActionBranding()
        alertVc.applyYesActionBranding()
        alertVc.addAction(noAction)
        alertVc.addAction(yesAction)
        
        alertVc.view.tintColor = .blue
        DispatchQueue.main.async(execute: {
            self.present(alertVc, animated: true, completion: nil)
        })
    }
    

    Hope It's work for all

提交回复
热议问题