0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kavin Kumar Arumugam
  • 1,792
  • 3
  • 28
  • 47

1 Answers1

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