title: Setting attributedText, NSRangeException error
body: <p>Trying to set my UIText view's attributed text properties via selection. Almost works. Action to set text with red font color below. This works sometimes, but often gives an error:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'NSRangeException', reason:
    'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'</p>
  </blockquote>
  
  <p>This happens even while there seems to be many more characters in the text view than indicated by the selected range.</p>
  
  <pre><code>- (IBAction)setText:(id)sender {
  
      NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithAttributedString:myTextView.attributedText];
      [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(p1,p2)];
  
      myTextView.attributedText = string;
  
  }
  </code></pre>
  
  <p>p1 and p2 are the start and end of the selected text. They are generated with the code below, which seems to work as expected:</p>
  
  <pre><code>- (void)textViewDidChangeSelection:(UITextView *)textView {
  
      UITextRange *selectedRange = [myTextView selectedTextRange];
  
      p1 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange start]];
      p2 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange end]];
  
   }
  </code></pre>
  
  <p><strong>EDIT: I fixed the problem after reading @borrrden's comment.</strong> Instead of <code>NSMakeRange(p1,p2)]</code> I am using <code>NSMakeRange(p1,p2-p1)]</code>.</p>