【问题标题】:Align Left AlertView Message in iOS8 Swift在 iOS8 Swift 中对齐左 AlertView 消息
【发布时间】:2015-05-27 12:02:21
【问题描述】:

我创建了一个警报视图,我想将警报视图的文本左对齐并使用此代码。

但问题是(alert.subviews).count0

这是我的代码:

let alert = UIAlertView(title: "Details:", message: "Composer: abcdfg \nShow:sdfa\nPerformer:asdasdf\nLanguage:farsi\nFirst Line:asdfasdfagadf", delegate: nil, cancelButtonTitle: "OK")
                   
for view in alert.subviews{
  if view.isKindOfClass(UILabel){
    (view as UILabel).textAlignment = NSTextAlignment.Left
   }
}
println((alert.subviews).count)
alert.show()

我想将消息左对齐,但警报的子视图计数为 0

【问题讨论】:

  • 已经看到这个但不想要自定义 uialert
  • 不要发布代码截图。将代码作为文本发布。图片可以从远程服务器上删除,如果我们想试试你的代码,复制/粘贴更容易。

标签: ios swift uialertview


【解决方案1】:

警报“条款和条件”的文本左对齐

 let alertController = UIAlertController(title: "iosGeek", message: "Terms and Condition", preferredStyle: .alert)
    let OKAction = UIAlertAction(title: "OK", style: .cancel) { (action) in
        alertController.dismiss(animated: true, completion: nil)
    }
    alertController.addAction(OKAction)

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.left

    let messageText = NSMutableAttributedString(
        string: "Terms and Condition",
        attributes: [
            NSParagraphStyleAttributeName: paragraphStyle,
            NSFontAttributeName: UIFont.systemFont(ofSize: 13.0)
        ]
    )

    alertController.setValue(messageText, forKey: "attributedMessage")
    self.present(alertController, animated: true, completion: nil)

【讨论】:

    【解决方案2】:

    swift 5 示例代码

        let alertMessage = """
        You message:
        • Point 1
        • Point 2
        • Point 3
        • Point 4
        """
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = NSTextAlignment.left
        
        let attributedMessageText = NSMutableAttributedString(
            string: alertMessage,
            attributes: [
                NSAttributedString.Key.paragraphStyle: paragraphStyle,
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0)
            ]
        )
        
        let alertController = UIAlertController.init(title: "Sample title", message: nil, preferredStyle: UIAlertController.Style.alert)
                
        // set attributed message here
        alertController.setValue(attributedMessageText, forKey: "attributedMessage")
    
        let actionBlock = UIAlertAction.init(title: "Block", style: UIAlertAction.Style.destructive) { (action) in
            self.callWebServiceBlockUserWithUserId(userId: self.arrUserBlocked[sender.tag].id, index: sender.tag)
        }
        
        let actionCancel = UIAlertAction.init(title: "Cancel", style: UIAlertAction.Style.cancel) { (action) in
        }
        
        
        alertController.addAction(actionBlock)
        alertController.addAction(actionCancel)
        
        self.present(alertController, animated: true, completion: nil)
    

    【讨论】:

      【解决方案3】:

      我发现这个要点可以创建一个带有左对齐文本的警报。致 gist 的作者——非常感谢:https://gist.github.com/nvkiet/29e4d5b5bef0865ee159

      本质上,在创建 UIAlertController 时:

      • 提供title

      message = 无

      • 按以下方式创建 NSMutableAttributedString:

      let paragraphStyle = NSMutableParagraphStyle()
      paragraphStyle.alignment = NSTextAlignment.left
      let attributedMessage: NSMutableAttributedString = NSMutableAttributedString(
          string: message, // your string message here
          attributes: [
              NSParagraphStyleAttributeName: paragraphStyle,
              NSFontAttributeName: UIFont.systemFont(ofSize: 13.0)
          ]
      )
      

      • 将属性字符串添加为警报控制器上的键:

      let alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.alert)
      alert.setValue(attributedMessage, forKey: "attributedMessage")
      

      • 显示警报

      【讨论】:

        【解决方案4】:

        UIAlertView 不支持 textAlignment

        您必须查看自定义解决方案。

        https://github.com/wimagguc/ios-custom-alertview

        【讨论】:

          【解决方案5】:

          根据apple documentation,您无法自定义UIAlertView 的外观,请查看给定链接中的警报视图外观部分。

          我认为有两种方法可以解决这个问题

          1. 使用任何适合您要求的第三方组件。
          2. 请您的设计师以您使用警报视图的其他方式呈现信息。

          【讨论】:

            【解决方案6】:

            只需创建这个函数,然后在需要的地方调用它

            func showAlert(title: String, message: String) {
            
             let alertController = UIAlertController(title: title, message:
                    message, preferredStyle: .alert)
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = NSTextAlignment.left
                let attributedMessage: NSMutableAttributedString = NSMutableAttributedString(
                    string: message, // your string message here
                    attributes: [
                        NSAttributedString.Key.paragraphStyle: paragraphStyle,
                        NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0)
                    ]
                )
                alertController.setValue(attributedMessage, forKey: "attributedMessage")
            
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
            
                }))
                self.present(alertController, animated: true, completion: nil)
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-10-02
              • 1970-01-01
              • 2021-07-17
              • 1970-01-01
              • 2019-04-27
              • 2022-07-20
              • 2017-11-14
              相关资源
              最近更新 更多