I implemented a customized textview based on UITextView, trying to listen the text change event. So I implemented the UITextView's delegate:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
//It crashes!
NSString *result = [textView.text stringByReplacingCharactersInRange:range withString:text];
return YES;
}
In iOS 5 and iOS 6,the above code works fine. But in iOS 4.3, it crashes with the exception ***-[NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds
.
I printed all the variable values, found in iOS 4.3, sometimes the range is out of bound indeed: textView.text.length
is 111, but the range
is 113,0
. What's wrong with my code? Thanks!