Skip to content

Commit

Permalink
Fix setMarkedText oob issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyin1986 committed Jan 8, 2020
1 parent 234eca2 commit 757babb
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions YYText/YYTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1469,11 +1469,13 @@ - (void)_replaceRange:(YYTextRange *)range withText:(NSString *)text notifyToDel
if (notify) [_inputDelegate selectionDidChange:self];
}
}
if (notify) [_inputDelegate textWillChange:self];
NSRange newRange = NSMakeRange(range.asRange.location, text.length);
[_innerText replaceCharactersInRange:range.asRange withString:text];
[_innerText yy_removeDiscontinuousAttributesInRange:newRange];
if (notify) [_inputDelegate textDidChange:self];
if (range.asRange.location + range.asRange.length <= _innerText.length) {
if (notify) [_inputDelegate textWillChange:self];
NSRange newRange = NSMakeRange(range.asRange.location, text.length);
[_innerText replaceCharactersInRange:range.asRange withString:text];
[_innerText yy_removeDiscontinuousAttributesInRange:newRange];
if (notify) [_inputDelegate textDidChange:self];
}
}

/// Save current typing attributes to the attributes holder.
Expand Down Expand Up @@ -3307,11 +3309,15 @@ - (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRang
if (!markedText) markedText = @"";
if (_markedTextRange == nil) {
_markedTextRange = [YYTextRange rangeWithRange:NSMakeRange(_selectedTextRange.end.offset, markedText.length)];
[_innerText replaceCharactersInRange:NSMakeRange(_selectedTextRange.end.offset, 0) withString:markedText];
if (_selectedTextRange.end.offset <= _innerText.length) {
[_innerText replaceCharactersInRange:NSMakeRange(_selectedTextRange.end.offset, 0) withString:markedText];
}
_selectedTextRange = [YYTextRange rangeWithRange:NSMakeRange(_selectedTextRange.start.offset + selectedRange.location, selectedRange.length)];
} else {
_markedTextRange = [self _correctedTextRange:_markedTextRange];
[_innerText replaceCharactersInRange:_markedTextRange.asRange withString:markedText];
if (_markedTextRange.asRange.location + _markedTextRange.asRange.length <= _innerText.length) {
[_innerText replaceCharactersInRange:_markedTextRange.asRange withString:markedText];
}
_markedTextRange = [YYTextRange rangeWithRange:NSMakeRange(_markedTextRange.start.offset, markedText.length)];
_selectedTextRange = [YYTextRange rangeWithRange:NSMakeRange(_markedTextRange.start.offset + selectedRange.location, selectedRange.length)];
}
Expand Down

0 comments on commit 757babb

Please sign in to comment.