【问题标题】:Change title and message font of alert using UIAlertController in Swift在 Swift 中使用 UIAlertController 更改警报的标题和消息字体
【发布时间】:2014-11-06 10:31:50
【问题描述】:

我正在尝试更改使用 UIAlertController 显示的警报的标题和消息字体 我正在尝试使用 NSAttributedStirng,但它给出了编译器错误,即不能采用 NSAttributed 字符串而不是 Stirng 我尝试了类似于this

var title_attrs = [NSFontAttributeName : CustomFonts.HELVETICA_NEUE_MEDIUM_16]
var msg_attrs = [NSFontAttributeName : CustomFonts.HELVETICA_NEUE_REGULAR_14]
var title = NSMutableAttributedString(string:"Done", attributes:title_attrs)

var msg = NSMutableAttributedString(string:"The job is done ", attributes:msg_attrs)
let alertController = UIAlertController(title: title, message: title , preferredStyle: UIAlertControllerStyle.Alert)

有人可以指导我如何实现这一目标吗?

【问题讨论】:

    标签: swift ios8 uialertcontroller


    【解决方案1】:

    Swift 3 版本:

    extension UIAlertController {
        func changeFont(view: UIView, font:UIFont) {
            for item in view.subviews {
                if item.isKind(of: UICollectionView.self) {
                    let col = item as! UICollectionView
                    for  row in col.subviews{
                        changeFont(view: row, font: font)
                    }
                }
                if item.isKind(of: UILabel.self) {
                    let label = item as! UILabel
                    label.font = font
                }else {
                    changeFont(view: item, font: font)
                }
    
            }
        }
    
        open override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
            let font = YOUR_FONT
            changeFont(view: self.view, font: font! )
        }
    }
    

    【讨论】:

    • 你救了我的命!
    • 代码正在唤醒,但小问题是,当我点击并向下拖动手指时,它将再次变为默认字体大小。有什么想法吗?
    • 这种方法的问题是标题和消息标签文本大小没有区别。
    • 您可以查看标签的字体点大小,并相应地更改字体。
    • 我尝试了这个解决方案来增加 iPad 上的字体大小。它工作正常,但以非常小的宽度显示警报。如何增加警报宽度?
    【解决方案2】:

    我认为 Apple 从 API 中删除了属性标题和 -message。它从来都不是公众API 的一部分,所以如果你使用它,Apple 可能不会允许你的应用进入应用商店。

    您应该按原样使用 UIAlertController。如果您想对其进行一些自定义,请参阅此NSHipster post。如果您想要更多控制,请创建一个自定义视图来显示。

    【讨论】:

      【解决方案3】:
          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")
          alertController.setValue(myMutableString, forKey: "attributedMessage")
      

      【讨论】:

      • 它导致我在 Swift 5 上崩溃
      【解决方案4】:
      extension UIAlertController {
          func changeFont(view:UIView,font:UIFont) {
              for item in view.subviews {
                  if item.isKindOfClass(UICollectionView) {
                      let col = item as! UICollectionView
                      for  row in col.subviews{
                          changeFont(row, font: font)
                      }
                  }
                  if item.isKindOfClass(UILabel) {
                      let label = item as! UILabel
                      label.font = font
                  }else {
                      changeFont(item, font: font)
                  }
      
              }
          }
          public override func viewWillLayoutSubviews() {
              super.viewWillLayoutSubviews()
              let font = UIFont(name: YourFontName, size: YourFontSize)
              changeFont(self.view, font: font! )
          }
      }
      

      【讨论】:

      • 重复的答案。
      猜你喜欢
      • 2016-10-19
      • 1970-01-01
      • 2019-02-05
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      相关资源
      最近更新 更多