title: AttributedText in TextView while typing
body: <p>I have a textView, and I am trying to give it an attributed text. I tried achieving it inside <code>shouldChangeTextInRange</code>, but it crashes for range out of index.</p>
  
  <pre><code>func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -&gt; Bool { 
  
     if myTextView {
        textView.attributedText = addAttributedText(1, text: text, fontsize: 13)
  
        let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
        let numberOfChars = newText.characters.count
  
        return numberOfChars &lt; 20
     }
     return true
  }
  
   func addAttributedText(spacing:CGFloat, text:String, fontsize: CGFloat) -&gt; NSMutableAttributedString {
      let attributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName:UIFont(
          name: "Font",
          size: fontsize)!])
      attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSMakeRange(0, text.characters.count))
      return attributedString
  
  }
  </code></pre>
  
  <p>I tried adding attributedString with empty text to textView in viewDidLoad, but that doesn't help. That's why I thought it would be appropriate to do it on <code>shouldChangeTextInRange</code> </p>
  
  <p>(Please note that my <code>addAttributedText</code> method works perfectly for other textviews)</p>
  
  <p>If I use this, in one character type-in, it writes 2x and crashes. What is the right way of handling that kind of converting textView's text to attributed text that is being typed.</p>