目标c超出范围的例外

时间:2022-02-15 16:44:02

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

我试图删除uitextview中突出显示的文本。例如,如果我有文本

"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'

'NSRangeException',原因:' - [__ NSCFString substringWithRange:]:范围{15,0}越界;弦长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];

}

1 个解决方案

#1


-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.

每个人都感谢你的帮助。即使我知道问题是我无法分辨的范围,显然textViewDidChangeSelection代表在触发textViewDidChange后连续被调用了2次。

#1


-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.

每个人都感谢你的帮助。即使我知道问题是我无法分辨的范围,显然textViewDidChangeSelection代表在触发textViewDidChange后连续被调用了2次。