更改 UIAlertController 的标题字体大小

Jac*_*erg 5 nsmutableattributedstring swift uialertviewcontroller swift4

我试图更改标题fontSize中的UIAlertController,但我不能管理如何我设置NSMutableAttributedStringtitle-属性。

所以我一直在NSMutableAttributedString使用以下代码创建:

let title = NSMutableAttributedString(string: user.fullName)
let range = NSRange(location: 0, length: title.length)
title.addAttribute(NSAttributedStringKey.font, value: UIFont.TextStyle.largeTitle, range: range)
Run Code Online (Sandbox Code Playgroud)

现在对我来说棘手的部分是如何弄清楚如何将新标题设置为UIAlertController,因为它需要一个String?值。

我环顾四周,发现UILabel在呈现UIAlertController. 但是如何使用我自己的自定义覆盖title-property ?UIAlertControllerUILabel

present(myUIAlertController, animated: true) {
    // Creating the UILabel depending on string length
    // Set the NSMutableAttributedString value to the custom UILabel and override title property.
}
Run Code Online (Sandbox Code Playgroud)

或者也许有更简单的方法来解决这个问题?


我的目标是像下图一样:

带有大标题的 UIAlertViewController

Sun*_*nny 9

您可以UIAlertController使用此代码制作属性的标题和消息。您可以根据需要进行自定义。您可以在图像中看到结果。我不确定你可以把它放在 Appstore 上。

func showAlert() {
  let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)        
  let titleAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 25)!, NSAttributedStringKey.foregroundColor: UIColor.black]
  let titleString = NSAttributedString(string: "Name Last name", attributes: titleAttributes)     
  let messageAttributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!, NSAttributedStringKey.foregroundColor: UIColor.red]
  let messageString = NSAttributedString(string: "Company name", attributes: messageAttributes)
  alert.setValue(titleString, forKey: "attributedTitle")
  alert.setValue(messageString, forKey: "attributedMessage")
  let labelAction = UIAlertAction(title: "Label", style: .default, handler: nil)
  let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  alert.addAction(labelAction)
  alert.addAction(deleteAction)
  alert.addAction(cancelAction)
  self.navigationController?.present(alert, animated: true, completion: nil)

}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明