2017-08-13 124 views
0

如何將操作表中UIAlertAction標題的文本對齊方式更改爲swift語言的左側?如何更改文本 - 在UIAlertAction中的左側對齊位置

下面

是我的代碼:

let alertController = UIAlertController() 
     let alertImage = UIAlertAction(title: "Image", style: .default, handler: nil) 
     let alertVideo = UIAlertAction(title: "Video", style: .default, handler: nil) 

     let alertFile = UIAlertAction(title: "File", style: .default, handler: nil) 
     let alertCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 

     let paragraphStyle = NSMutableParagraphStyle() 
     // Here is the key thing! 
     paragraphStyle.alignment = .left 

     let messageText = NSMutableAttributedString(
      string: "Video", 
      attributes: [ 
       NSParagraphStyleAttributeName: paragraphStyle, 
       NSFontAttributeName : UIFont.preferredFont(forTextStyle: .subheadline), 
       NSForegroundColorAttributeName : UIColor.black 
      ] 
     ) 

     // add icon to the left alert action 
     let image = UIImage(named: "video") 
     alertVideo.setValue(image, forKey: "image") 
     // align text in the alertAction 
     alertVideo.setValue(UIColor.black, forKey: "titleTextColor") 
     alertVideo.setValue(messageText, forKey: "attributedTitle") 

     alertController.addAction(alertImage) 
     alertController.addAction(alertVideo) 
     alertController.addAction(alertFile) 
     alertController.addAction(alertCancel) 

     present(alertController, animated: true, completion: nil) 

我得到的錯誤信息是

[<UIAlertAction 0x7a6eb760> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key attributedTitle 

我怎樣才能解決這個問題,所以我可以在動作片更改警報措施文本對齊

非常感謝你

+1

我90%肯定,你需要做一個自定義的警報視圖這是因爲系統警報觀點都有其默認樣式的硬連線 –

回答

相關問題