title: How to allow NSAttributedString text be typed into a UITextView?
body: <p>I'm trying to allow different styles of text to be typed into a UITextView, a bit like a text editor using simple attributes such as bold or italics. I understand by using the textView's <code>attributedText</code> property I can apply attributes to certain ranges of text. This is nice, but I would like to be able to type attributed text into the textView, which would be toggled by a button (such as the typing of bold text).</p>
  
  <p>Here's what I've thought of so far:</p>
  
  <p>I've used the <code>-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text</code> UITextView delegate method to take the <code>text</code> argument, and modify it with attributes by creating an NSAttributedString with the same text. Then create a NSMutableAttributedString, which is a copy of the <code>textView</code>'s attributedText. Append the two using <code>appendAttributedString</code>, and then set the textView's <code>attributedText</code> property to the resulting attributedString.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  
     if (self.boldPressed) {
  
          UIFont *boldFont = [UIFont boldSystemFontOfSize:self.textView.font.pointSize];
          NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
          NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:text attributes:boldAttr];
          NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
          [textViewText appendAttributedString:attributedText];
          textView.attributedText = textViewText;
  
             return NO;
         }
     return YES;                
  }
  </code></pre>
  
  <p>Having to reset the textViews attributedText every time a character is typed seems like a bit much for a simple action. Not only that, but it doesn't work properly. Here's what it looks like when the bold attribute is enabled:</p>
  
  <p><img src="https://i.stack.imgur.com/tHRsY.png" alt="attributedText issues"></p>
  
  <p>There are two problems with this. The most obvious being how every new character is put onto a new line. But what's also strange is that the insertion point is always at the very first index of the text in the textview (only when bold is enabled, yet the bold character is inserted on a new line). So if you're typing with bold enabled, and then turn bold off, typing resumes in front of all existing text.</p>
  
  <p>I'm not sure why these error are happening. I also just don't think that my solution is very efficient, but I can't think of any other way of implementing it.</p>