14

I followed the following post on how to use NSTextAttachment to add images inline with your UILabels. I followed the best I could and wrote my version in Swift.

I am creating a chat application and the field that I'm inserting a beer icon into does not render the image or doesn't seem to render the image inline. I don't get any errors so I'm assuming I'm missing some small little detail in my code.

var beerName:String!

        if(sender == bn_beer1)
        {
            beerName = "beer1.png"
        }

        if(sender == bn_beer2)
        {
            beerName = "beer2.png"
        }

        if(sender == bn_beer3)
        {
            beerName = "beer3"
        }



        var attachment:NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: beerName)


        var attachmentString:NSAttributedString = NSAttributedString(attachment: attachment)
        var myString:NSMutableAttributedString = NSMutableAttributedString(string: inputField.text)
        myString.appendAttributedString(attachmentString)



        inputField.attributedText = myString;
Community
  • 1
  • 1
mattwallace
  • 3,687
  • 4
  • 35
  • 69

4 Answers4

35

This does not work on a UITextField. It only works on a UILabel.

Here is a UILabel extension based on your code (Swift 2.0)

extension UILabel
{
    func addImage(imageName: String)
    {
        let attachment:NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: imageName)

        let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment)
        let myString:NSMutableAttributedString = NSMutableAttributedString(string: self.text!)
        myString.appendAttributedString(attachmentString)

        self.attributedText = myString
    }
}

EDIT:

here is a new version that allow to add the icon before or after the label. There is also a function to remove the icon from the label

extension UILabel
{
    func addImage(imageName: String, afterLabel bolAfterLabel: Bool = false)
    {
        let attachment: NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: imageName)
        let attachmentString: NSAttributedString = NSAttributedString(attachment: attachment)

        if (bolAfterLabel)
        {
            let strLabelText: NSMutableAttributedString = NSMutableAttributedString(string: self.text!)
            strLabelText.appendAttributedString(attachmentString)

            self.attributedText = strLabelText
        }
        else
        {
            let strLabelText: NSAttributedString = NSAttributedString(string: self.text!)
            let mutableAttachmentString: NSMutableAttributedString = NSMutableAttributedString(attributedString: attachmentString)
            mutableAttachmentString.appendAttributedString(strLabelText)

            self.attributedText = mutableAttachmentString
        }
    }

    func removeImage()
    {
        let text = self.text
        self.attributedText = nil
        self.text = text
    }
}
Regis St-Gelais
  • 2,976
  • 5
  • 24
  • 38
  • If you are Planning on Adding multiple images, Remember to change the Initializer from let strLabelText: NSAttributedString = NSAttributedString(string: self.text!) to: let strLabelText: NSMutableAttributedString = NSMutableAttributedString(attributedString: self.attributedText!) – Hernan Arber Feb 28 '17 at 09:24
  • 1
    @HernanArber, How do you implement multiple images? – Coder221 Apr 29 '19 at 00:02
14

Regis St-Gelais's extension answer for Swift 3 and Swift 4 and without forced unwrapping:

extension UILabel {

    func addImageWith(name: String, behindText: Bool) {

        let attachment = NSTextAttachment()
        attachment.image = UIImage(named: name)
        let attachmentString = NSAttributedString(attachment: attachment)

        guard let txt = self.text else {
            return
        }

        if behindText {
            let strLabelText = NSMutableAttributedString(string: txt)
            strLabelText.append(attachmentString)
            self.attributedText = strLabelText
        } else {
            let strLabelText = NSAttributedString(string: txt)
            let mutableAttachmentString = NSMutableAttributedString(attributedString: attachmentString)
            mutableAttachmentString.append(strLabelText)
            self.attributedText = mutableAttachmentString
        }
    }

    func removeImage() {
        let text = self.text
        self.attributedText = nil
        self.text = text
    }
}

Usage:

self.theLabel.text = "desiredText"
self.theLabel.addImageWith(name: "nameOfImage", behindText: false)
Komal12
  • 3,119
  • 4
  • 13
  • 25
David Seek
  • 15,533
  • 14
  • 94
  • 125
8

Edit 19/03/18 : Correct bug when imageBehindText = false + Image size in pixel no point.

David's function update for multiple images with text saving and image size based on font size (Swift 4) :

extension UILabel {
    /**
     This function adding image with text on label.

     - parameter text: The text to add
     - parameter image: The image to add
     - parameter imageBehindText: A boolean value that indicate if the imaga is behind text or not
     - parameter keepPreviousText: A boolean value that indicate if the function keep the actual text or not
     */
    func addTextWithImage(text: String, image: UIImage, imageBehindText: Bool, keepPreviousText: Bool) {
                    let lAttachment = NSTextAttachment()
        lAttachment.image = image

        // 1pt = 1.32px
        let lFontSize = round(self.font.pointSize * 1.32)
        let lRatio = image.size.width / image.size.height

        lAttachment.bounds = CGRect(x: 0, y: ((self.font.capHeight - lFontSize) / 2).rounded(), width: lRatio * lFontSize, height: lFontSize)

        let lAttachmentString = NSAttributedString(attachment: lAttachment)

        if imageBehindText {
            let lStrLabelText: NSMutableAttributedString

            if keepPreviousText, let lCurrentAttributedString = self.attributedText {
                lStrLabelText = NSMutableAttributedString(attributedString: lCurrentAttributedString)
                lStrLabelText.append(NSMutableAttributedString(string: text))
            } else {
                lStrLabelText = NSMutableAttributedString(string: text)
            }

            lStrLabelText.append(lAttachmentString)
            self.attributedText = lStrLabelText
        } else {
            let lStrLabelText: NSMutableAttributedString

            if keepPreviousText, let lCurrentAttributedString = self.attributedText {
                lStrLabelText = NSMutableAttributedString(attributedString: lCurrentAttributedString)
                lStrLabelText.append(NSMutableAttributedString(attributedString: lAttachmentString))
                lStrLabelText.append(NSMutableAttributedString(string: text))
            } else {
                lStrLabelText = NSMutableAttributedString(attributedString: lAttachmentString)
                lStrLabelText.append(NSMutableAttributedString(string: text))
            }

            self.attributedText = lStrLabelText
        }
    }

    func removeImage() {
        let text = self.text
        self.attributedText = nil
        self.text = text
    }
}
Jordan Favray
  • 143
  • 1
  • 11
0

Use these below extension it will work for sure

 extension NSMutableAttributedString { 
   static func addImageInBetweenString(firstSentence: String, image: UIImage?, lastSentence: String) -> NSMutableAttributedString {
     // create an NSMutableAttributedString that we'll append everything to
     let fullString = NSMutableAttributedString(string: firstSentence)

     // create our NSTextAttachment
     let image1Attachment = NSTextAttachment()
     image1Attachment.image = image
     //image1Attachment.setImageHeight(height: 15.0)
     Image1Attachment.bounds = CGRect(x: 0, y: -5, width: 15, height: 15)

     // wrap the attachment in its own attributed string so we can append it
     let image1String = NSAttributedString(attachment: image1Attachment)

    // add the NSTextAttachment wrapper to our full string, then add some more text.
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: lastSentence))

    return fullString
   }
} 



extension NSTextAttachment {
   func setImageHeight(height: CGFloat) {
      guard let image = image else { return }
      let ratio = image.size.width / image.size.height
      bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: ratio * height, height: height)
  }
 }
Purnendu roy
  • 718
  • 7
  • 11