title: Keep text and image together in AttributedString
body: <p>I have an attributed string in Swift which displays an icon next to a username. This works great, my implementation looks like this:</p>
  
  <pre><code>attributedUsername = NSMutableAttributedString(string: "username")
  let iconAttachment = NSTextAttachment()
  let iconImage = UIImage(named: "userIcon")
  iconAttachment.image = iconImage
  iconAttachment.bounds = CGRect(x: 0, y: -3, width: 14, height: 14)
  let iconString = NSAttributedString(attachment: verifiedAttachment)
  attributedUsername.append(iconString)
  
  usernameLabel.attributedText = attributedUsername
  </code></pre>
  
  <p>However, sometimes a username is too large to fit on one line, wrapping the username on a second line (<code>numberOfLines = 0</code>). This is OK, but if the username is just long enough to fit on-screen, then the image wraps to the next line. I am wondering if there is any way to keep the icon wrapped to the end of the username. What I am looking to achieve, where * is the icon, is:</p>
  
  <pre><code>username *
  
  longer username *
  
  a very long
  username *
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>username *
  
  longer username *
  
  a very long username
  *
  </code></pre>
  
  <p>So basically I want the icon to stick together with the last part of the username (if possible). If the username doesn't contain spaces and is too long, then it should just be wrapped on the next line because that would be standard implementation. Any suggestions?</p>