gpt4 book ai didi

swift - 将 SF Symbols 添加到 UIAlert 的标题中

转载 作者:行者123 更新时间:2023-12-05 01:37:07 25 4
gpt4 key购买 nike

我在将 SF 符号添加到标题时遇到问题。 SF Symbols 与标题文本重叠有人可以帮助我吗

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)
}

最佳答案

您可以使用 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

有关属性字符串的更多信息,请参阅 Paul Hudson 撰写的一篇关于它们的精彩文章:https://www.hackingwithswift.com/articles/113/nsattributedstring-by-example

关于swift - 将 SF Symbols 添加到 UIAlert 的标题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61374747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com