我想删除由回车键输入的换行符。但是当我执行以下操作时,它会从文本中删除最后一个字符。为什么?
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
NSLog(@"%d %d %@ %@",range.location,range.length, text, [textView text]);
if ( [text isEqualToString:@"\n"] ) {
NSString *s = [textView text];
s = [s substringToIndex:[s length] - 1]; // <------------
[tvText setText:[NSString stringWithFormat:@"%@\n>>",s]];
}
return YES;
}
我希望结果看起来像:
>>name
>>yoda
>> <---- cursor is moved to the right of ">>"