0

I am trying to delete the highlighted text inside of a uitextview. For example If I had the text

"I am going home"

If I want to delete the word "going"

I get this error

'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {15, 0} out of bounds; string length 13'

Edit:

Error occurs on this line

[textViewText replaceCharactersInRange:range withAttributedString:formatString]

Here are the variable values

range = {4,1} //{_rangestart, _rangeEnd}
formatString= " "
textViewText="I am  home" //extra space there


  {
      NSMutableAttributedString *formatString;

  if(_rangeEnd == 0)
  {
    _rangeStart -=1;
    _rangeEnd = 1;
  }
  else if(_rangeEnd > 1)
  {
   // _rangeStart -=1;
  }
  else
  {
    _rangeStart +=1;
  }


NSRange range = NSMakeRange(_rangeStart, _rangeEnd);

  NSDictionary *attrDict = @{
                             NSFontAttributeName : _boldFont,
                             NSFontAttributeName : _italicFont,
                             NSForegroundColorAttributeName : _color,
                             NSBackgroundColorAttributeName: _backgroundColor,
                             };

  NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];

  formatString = [[NSMutableAttributedString alloc]initWithString:_selectedText attributes:attrDict];

  if(_underline == YES)
  {
  [formatString addAttribute:NSUnderlineStyleAttributeName
                          value:[NSNumber numberWithInt:1]
                          range:(NSRange){0,[formatString length]}];
  }


  if(_strikeThrough == YES)
  {
  [formatString addAttribute:NSStrikethroughStyleAttributeName
                       value:[NSNumber numberWithInt:1]
                       range:(NSRange){0,[formatString length]}];
  }

  NSNumber *location = [NSNumber numberWithInteger:range.location];

    if(location < 0 || location == nil)
    {
      range.location=0;
    }

     if(textViewText.length > 0 && formatString.length > 0)
     {
    [textViewText replaceCharactersInRange:range withAttributedString:formatString];

    dispatch_async(dispatch_get_main_queue(), ^{
        textView.attributedText = textViewText;
        [textView setTextAlignment:_alignment];
    });
      }

This is the delegate calling the function above

    - (void)textViewDidChange:(UITextView *)textView{
  _textV = textView;
  _selectedText = [textView.text substringWithRange:textView.selectedRange];
  _rangeStart = textView.selectedRange.location;
  _rangeEnd = textView.selectedRange.length;

  if([_selectedText isEqual:@""] && _rangeEnd == 0)
  {
    _selectedText = [textView.text substringWithRange:NSMakeRange(_rangeStart - 1, 1)];
    [self applyBold:textView];
  }


  NSDictionary *event = @{
                          @"target": textView.reactTag,
                          @"highlights": @{
                              @"text": textView.text,
                              @"range": @(textView.selectedRange.length),
                              @"cursorPosition": @(textView.selectedRange.location),
                              @"eventType": @"textViewDidChange",

                              }
                          };
  [self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];

}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Train
  • 3,420
  • 2
  • 29
  • 59
  • 1
    where *exactly* does the error occur? – luk2302 Dec 31 '15 at 15:12
  • Without knowing what sets `_rangeStart` and `_rangeEnd` and to what values, how can anyone help you? – trojanfoe Dec 31 '15 at 15:51
  • Are you asking for an explanation on what the error is saying? If so, you are using a range on a string that is to short or otherwise only 13 in length, while your range starts at index 15. Your string is thus, NOT "I am going home". Try to print out the string with NSLog before applying the range. – Sverrisson Dec 31 '15 at 16:29
  • 1
    You are aware that the arguments to `NSMakeRange` are start and length, not start and end, right? – jcaron Dec 31 '15 at 16:35
  • yes I am aware that end=length in my case. – Train Dec 31 '15 at 16:37
  • 1
    You should name it length, then, it would make more sense for everybody, including you. – jcaron Dec 31 '15 at 16:41
  • Also, you're comparing an `NSNumber` (`location`) with an integer (`location < 0`). Definitely won't do anything sensible. – jcaron Dec 31 '15 at 16:42
  • 1
    Can you provide the actual values of `textView`, `range` and the error message that match together? Make sure those are the actual values when `replaceCharactersInRange:withAttributedString:` is called, not what you think they are because you logged them at some point earlier in your impenetrable logic. – jcaron Dec 31 '15 at 16:44

1 Answers1

-1

Everyone thank you for your help. even though I knew the problem was with the range I couldn't tell why, apparently the textViewDidChangeSelection Delegate was being called 2 times in a row after the textViewDidChange was being fired.

Train
  • 3,420
  • 2
  • 29
  • 59