【问题标题】:UIAlertAction Button Text Alignment LeftUIAlertAction 按钮文本左对齐
【发布时间】:2017-11-18 20:42:37
【问题描述】:

我想将UIAlertAction 文本对齐到左对齐并添加如图所示的图标。我在谷歌上花了很多时间来获得解决方案,但没有找到正确的答案。每个正文都针对警报标题而不是警报操作标题。

请为我推荐 swift 的正确方法。

谢谢!!

【问题讨论】:

  • 没有公共 API 可以使用 UIAlertController 执行此操作。

标签: ios swift uialertcontroller uialertaction


【解决方案1】:

您可以使用下面的示例代码来做到这一点。简单易行。

斯威夫特 4

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

let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheetAlertController.addAction(cancelActionButton)


let documentsActionButton = UIAlertAction(title: "Documents", style: .default, handler: nil)
actionSheetAlertController.addAction(documentsActionButton)
documentsActionButton.setValue(#imageLiteral(resourceName: "doccument"), forKey: "image")
documentsActionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")

let cameraActionButton = UIAlertAction(title: "Camera", style: .default, handler: nil)
actionSheetAlertController.addAction(cameraActionButton)
cameraActionButton.setValue(#imageLiteral(resourceName: "camera"), forKey: "image")
cameraActionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")

let galleryActionButton = UIAlertAction(title: "Gallery", style: .default, handler: nil)
actionSheetAlertController.addAction(galleryActionButton)
galleryActionButton.setValue(#imageLiteral(resourceName: "gallery"), forKey: "image")
galleryActionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")

actionSheetAlertController.view.tintColor = kTHEME_COLOR
self.present(actionSheetAlertController, animated: true, completion: nil)

【讨论】:

  • 单行代码 actionButton.setValue(kCAAlignmentLeft, forKey: "titleTextAlignment")
  • 'kCAAlignmentLeft' 已从 swift 5 重命名为 'CATextLayerAlignmentMode.left'
【解决方案2】:

kCAAlignmentLeft 的 swift 5.0 的更新版本(由@Niraj Solanki here 回答)是CATextLayerAlignmentMode.left 所以代码应该如下:

Swift 5.0

documentsActionButton.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")

【讨论】:

【解决方案3】:

您可以使用下面的示例代码来做到这一点。 用您的图片替换图片名称

斯威夫特 3

 let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
 let camera = UIAlertAction(title: "Camera", style: .default) { (action) in
  // Do something
 }
 let cameraImage = UIImage(named: "icon_camera")
 camera.setValue(cameraImage, forKey: "image")
 camera.setValue(0, forKey: "titleTextAlignment")
 actionSheet.addAction(camera)

【讨论】:

    【解决方案4】:

    正如在 cmets 中所说,UIAlertController 不提供对其视图的自定义。但是,您可以构建自己的操作表视图或使用一些第三方,例如 https://github.com/xmartlabs/XLActionController

    【讨论】:

      【解决方案5】:

      这种情况下最快和最简单的实现:

      extension UIAlertAction {
          func addImage(_ image: UIImage){
              setValue(image, forKey: "image")
              setValue(0, forKey: "titleTextAlignment")
          }
      }
      

      使用:

      let editAction = UIAlertAction(title: "Edit", style: .default, handler: nil)
      editAction.addImage(UIImage(systemName: "square.and.pencil")!)
      alertSheet.addAction(editAction)
      

      注意: CATextLayerAlignmentMode — 不是您需要的。因为右对齐不起作用!更好地使用数字文字:

      • 0 - 左
      • 1 - 中心
      • 2 - 右

      为了通用,我会创建这个枚举:

      enum AlertActionAlignment:Int {
          case left, center, right
      }
      
      ...
      setValue(align.rawValue, forKey: "titleTextAlignment")
      

      【讨论】: