In my application I need to display image as background for particular character within string. It is possible in Swift? How can I do that?
Asked
Active
Viewed 325 times
1 Answers
0
I suppose you are using UILabel. So try to do this:
// 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
I hope this can help you.

J. Lopes
- 1,336
- 16
- 27