簡體   English   中英

UIAlertAction 按鈕文本 Alignment 左

[英]UIAlertAction Button Text Alignment Left

我想將UIAlertAction文本 alignment 向左對齊並添加如圖所示的圖標。 我花了很多時間在谷歌上尋找解決方案,但沒有找到正確的答案。 警報標題的每個正文帖子都不是警報操作標題。 在此處輸入圖像描述

請建議我swift的正確方法。

謝謝!!

你可以用下面的示例代碼來做到這一點。 簡單易行。

斯威夫特 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)

kCAAlignmentLeft swift 5.0 的更新版本(由@Niraj Solanki在這里回答)是CATextLayerAlignmentMode.left所以代碼應該如下:

斯威夫特 5.0

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

你可以用下面的示例代碼來做到這一點。 用您的圖像替換圖像名稱

斯威夫特 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)

正如評論中已經說過的, UIAlertController 不提供其視圖的自定義。 但是,您可以構建自己的操作表視圖或使用某些第三方,例如https://github.com/xmartlabs/XLActionController

這種情況下最快和最簡單的實現:

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")

對於警報操作,您可以使用此代碼,

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

let camera = UIAlertAction(title: "Camera", style: .default) { action in
            //add alert action
        }

camera.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
let icon = UIImage.(named: "your image file name")

camera.setValue(icon, forKey: "image")

actionSheet.addAction(camera)

present(actionSheet, animated: true)

使用此代碼,您可以實現帶有圖像和左對齊警報操作文本的警報操作表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM