добавление символов SF к названию UIAlert - PullRequest
0 голосов
/ 22 апреля 2020

У меня проблема с добавлением символов SF в заголовок. Символы SF пересекаются с текстом заголовка, может кто-нибудь мне помочь

enter image description here

func uialert(){

    let alert = UIAlertController(title: "New User Created", message: " A new user has been created.", preferredStyle: .alert)
    let imgTitle = UIImage(systemName: "checkmark.circle")
    let imgViewTitle = UIImageView(frame: CGRect(x: 120, y: 10, width: 30, height: 30))
    imgViewTitle.image = imgTitle
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    alert.view.addSubview(imgViewTitle)
    self.present(alert, animated: true)
}

1 Ответ

1 голос
/ 23 апреля 2020

Вы можете использовать NSAttributedString, чтобы установить там галочку. Вот код:

// 1. create the image for the attributed string
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(systemName: "checkmark.circle") 

// 2. Create the attributed string and append the image
let fullString = NSMutableAttributedString(string: "New User Created ")
fullString.append(NSAttributedString(attachment: imageAttachment))

// 3. Make the alert with an empty title and set the attributedTitle using .setValue
let alert = UIAlertController(title: "", message: " A new user has been created.", preferredStyle: .alert)
alert.setValue(fullString, forKey: "attributedTitle")

// 4. Present the alert
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)

И результат:

successful alert

Для получения дополнительной информации об атрибутных строках вот отличная статья о них Пол Хадсон: https://www.hackingwithswift.com/articles/113/nsattributedstring-by-example

...