将图像添加到iOS Swift中的文本背景



在我的应用程序中,我需要将图像显示为字符串中特定字符的背景。有可能在Swift中吗?我该怎么做?

我想您正在使用uilabel。因此,尝试这样做:

// Create an NSMutableAttributedString
let mainString = NSMutableAttributedString(string: "Your Text")
// Create NSTextAttachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "image.png")
// Attachment imageAttachment to NSAttributedString
let imageString = NSAttributedString(attachment: imageAttachment)
// Add the NSTextAttachment to the mainString
mainString.append(imageString)
mainString.append(NSAttributedString(string: "End"))
// Set the result in your label
yourLabel.attributedText = mainString

我希望这可以帮助您。

最新更新