Aliunco
Aliunco

Reputation: 399

How to change font of buttons in UIAlertController

I need to change font of buttons in UIAlertController, I've used the code below, but I doesn't work properly, because when the user touches the buttons of UIAlertController, the font will be change to it's default.

extension UIAlertController {

    private func changeFont(view:UIView,font:UIFont) {
        for item in view.subviews {
            if let col = item as? UICollectionView {
                for  row in col.subviews{
                    changeFont(view: row, font: font)
                }
            }
            if let label  = item as? UILabel {
                label.font = font
            } else {
                changeFont(view: item, font: font)
            }

         }
     }

     //To set font for any UILabels in action sheet
     open override func viewWillLayoutSubviews() {
         super.viewWillLayoutSubviews()
         let font = MyCustomFont
         changeFont(view: self.view, font: font! )
     }
}

and here is the usage of this extension:

    let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let action = UIAlertAction(title: STRING_OF_ACTION, style: .default) { _ in
        //some action code
    }
    optionMenu.addAction(action)

    //in UIViewController 
    self.present(optionMenu, animated: true, completion: nil)

Upvotes: 3

Views: 1866

Answers (1)

sunkehappy
sunkehappy

Reputation: 9091

It's hard to change it because the iOS system doesn't provide the API for us to do it. So we'd better use third party libraries like: SCLAlertView-Swift or PopupDialog if we need some customization.

Upvotes: 2

Related Questions