1

I'm trying to add an image as NSTextAttachment to UITextField, but all I get is ,

Code within custom UITextField:

let myAttachment = NSTextAttachment()
myAttachment.image = myImage
myAttachment.bounds = CGRect(origin: .zero, size: myImage.size)

let myImageString = NSAttributedString(attachment: myAttachment)
let updatedText = NSMutableAttributedString()
updatedText.append(myImageString)

let myTextString = NSAttributedString(string: ", " + (self.text ?? ""))

updatedText.append(myTextString)

self.attributedText = updatedText
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
zzzkk
  • 49
  • 1
  • 10

2 Answers2

1

You are correct, NSTextAttachment isn't appearing in UITextField, I am unsure if its a bug or by design of UITextField, I will file a radar and update the response here:

Meanwhile, if you can change UI element to UILabel and UITextView, following works file:

let attachment = NSTextAttachment()
attachment.image = myImage
attachment.bounds = CGRect(origin: .zero, size: myImage.size)

let attributedStr = NSMutableAttributedString(string: self.text ?? "")
attributedStr.insert(NSAttributedString(attachment: attachment), at: 0)

enter image description here

AamirR
  • 11,672
  • 4
  • 59
  • 73
1

UITextField will not allow NSTextAttachment but UILabel allows the NSTextAttachment.

enter image description here

For Example:

let attachment = NSTextAttachment()
let imageTest = UIImage(named:"user.png")
attachment.image = imageTest
let myString = NSMutableAttributedString(string: "My text ")
let myStringWithImage = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
myStringWithImage.append(myString)
myTextField.attributedText = myStringWithImage
myLabel.attributedText = myStringWithImage

No answers in this apple thread. As of my thought we can enter emojis in text field but not text attachment. In text field we are giving flexibility to user for entering text.

You can use the leftView of UITextField to display image.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • @AamirR I am also looking for the same but nothing is available. As of my thought we can enter emojis in text field but not text attachment. – Ashish Kakkad Feb 21 '19 at 11:15
  • If it is true thats fine, but I wonder why there is a lot of examples on the internet which show how to add an image attachment to text view? – zzzkk Feb 21 '19 at 11:34
  • @zzzkk P.S. TextField and TextView is different. If you have examples please share. – Ashish Kakkad Feb 21 '19 at 11:39
  • ahh, yes, I mean text field :) this one fooled me but for `attributedPlaceholder` it really works https://stackoverflow.com/questions/34897748/uitextfield-with-a-placeholder-containing-an-image-and-text so maybe there is not really a lot of them as I said before – zzzkk Feb 21 '19 at 12:17
  • This is not true. UITextField does allow NSTextAttachment – Rufat Mirza Jan 12 '20 at 19:37
  • @RufatMirza Post an answer with your code. This answer 1 year old at that time this is the case. – Ashish Kakkad Jan 15 '20 at 04:16