在YYTextView.m里1471,3347,3351行加越界判断即可:
1471:
if(range.asRange.location+range.asRange.length<=_innerText.length) {
if(notify) [_inputDelegatetextWillChange:self];
NSRangenewRange =NSMakeRange(range.asRange.location, text.length);
[_innerText replaceCharactersInRange:range.asRange withString:text];
[_innerText yy_removeDiscontinuousAttributesInRange:newRange];
if(notify) [_inputDelegatetextDidChange:self];
}
3347:
if (_markedTextRange == nil) {
_markedTextRange = [YYTextRange rangeWithRange:NSMakeRange(_selectedTextRange.end.offset, markedText.length)];
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];
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)];
}