title: How to add images as text attachment in Swift using NSAttributedString?
body: <p>I'm trying to build a custom keyboard for iOS using images which I've put in as buttons. When I press a button, the image linked to the button is put into an attributed string which is loaded into an UiTextView inside the custom keyboard view. That is working.</p>
  <p>The problem is that when I append a new image to the attributed string both the old and new images in the string are changing to the image I currently pressed on. I can't understand why the old images in the string are changing.</p>
  <p>Any suggestions? I've tried using <code>replaceCharactersInRange</code> and <code>insertAttributedString</code> but can't get it to work. Here is the code (after viewDidLoad):</p>
  <pre><code>let textAttachment = NSTextAttachment()
  
  let textView = UITextView(frame: CGRectMake(5, 5, 200, 40))
  var attributedString = NSMutableAttributedString(string: &quot;&quot;)
      
  @IBAction func buttonPressed(button :UIButton) {
            
      let string = button.titleLabel?.text
      
      textAttachment.image = UIImage(named: &quot;\(string!).png&quot;)!
      textAttachment.image = UIImage(CGImage: textAttachment.image!.CGImage!, scale: 6, orientation: .Up)
      let attrStringWithImage = NSAttributedString(attachment: textAttachment)
      attributedString.appendAttributedString(attrStringWithImage);
      
      textView.attributedText = attributedString;
  }
  </code></pre>