【问题标题】:How to change font of buttons in UIAlertController如何更改 UIAlertController 中按钮的字体
【发布时间】:2017-11-01 05:31:20
【问题描述】:

我需要更改UIAlertController中按钮的字体,我使用了下面的代码,但是我不能正常工作,因为当用户触摸UIAlertController的按钮时,字体会变成默认字体。

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! )
     }
}

这里是这个扩展的用法:

    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)

【问题讨论】:

    标签: swift swift3 uialertcontroller


    【解决方案1】:

    很难改,因为iOS系统没有提供API让我们改。所以我们最好使用第三方库,比如:SCLAlertView-SwiftPopupDialog,如果我们需要一些定制的话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2019-02-05
      相关资源
      最近更新 更多