I have a problem adding SF Symbols to the title. SF Symbols is overlap with title text can someone help me
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)
}
You can use NSAttributedString to get the checkmark in there. Here's the code:
// 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)
And the result:
For more info on attributed strings here's a great article on them by Paul Hudson: https://www.hackingwithswift.com/articles/113/nsattributedstring-by-example