Изменить заголовок UIAlertController
Я пытаюсь сменить название fontSize
в UIAlertController
, но я не могу управлять, как настроить мой NSMutableAttributedString
к title
-имущество.
Так что я создавал 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)
Теперь для меня сложнее понять, как установить новое название для UIAlertController
потому что он ожидает String?
значение.
Я огляделся и обнаружил, что, вероятно, мне следует создать UILabel
в блоке завершения при представлении UIAlertController
, Но как мне переопределить title
собственность в UIAlertController
с моим собственным обычаем UILabel
?
present(myUIAlertController, animated: true) {
// Creating the UILabel depending on string length
// Set the NSMutableAttributedString value to the custom UILabel and override title property.
}
Или, может быть, есть даже более простой способ решить эту проблему?
Моя цель состоит в том, чтобы иметь как изображение ниже:
2 ответа
Вы можете сделать заголовок и сообщение 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)
}
Нет другого способа, кроме использования частного API.
Я могу предложить вам создать свой AlertViewController со свойствами, которые вы хотите настроить.