title: How to maintain the position of the UITextview with NSMutableAttributedString after changing font attriibutes
body: <p>I am allowing the user to do formatting of the <code>NSMutableAttributedString</code> text inside a <code>UITextView</code>. Formatting examples are: Bold, Italic, Change font etc.</p>
  
  <p>The attribute changes are reflected correctly. However after the <code>setAttributedText</code> and <code>invalidateDisplayForCharacterRange</code>, my <code>UITextView</code> ends up being scrolled to the very bottom of the text. How do I replace the attributes of the text without changing the position of the UITextView?</p>
  
  <p>CODE:</p>
  
  <pre><code>- (void)placeCursorAfterFormatting
  {
      self.mytextview.selectedRange=currentRangeForFormatting;
  }
  
  - (IBAction)boldclicked:(UIButton *)sender {
      currentRangeForFormatting = [self.mytextview selectedRange];
  
      NSMutableAttributedString *atstr = [[[self.mytextview.attributedText mutableCopy] attributedSubstringFromRange:currentRangeForFormatting] mutableCopy];
      [atstr enumerateAttribute:NSFontAttributeName inRange:(NSRange){0,[atstr length]} options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
          [atstr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AvenirNext-Bold" size:18] range:range];
      }];
      NSMutableAttributedString *atstrnew = [self.mytextview.attributedText mutableCopy];
      [atstrnew replaceCharactersInRange:currentRangeForFormatting withAttributedString:atstr];
      [self.mytextview setAttributedText:atstrnew];
      [self.mytextview.layoutManager invalidateDisplayForCharacterRange:currentRangeForFormatting];
  
      [self performSelector:@selector(placeCursorAfterFormatting) withObject:nil afterDelay:0.1];
  
  }
  </code></pre>