我想删除按回车键输入的换行符。但是当我执行以下操作时,它会从文本中删除最后一个字符。为什么?
- (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 ">>"
最佳答案
我认为你可以做这样的事情,
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
NSLog(@"%d %d %@ %@",range.location,range.length, text, [textView text]);
if ( [text isEqualToString:@"\n"] ) {
[tvText setText:[NSString stringWithFormat:@"%@\n>>",tvText.text]];
return NO;
}
return YES;
}
关于objective-c - UITextView,如何删除返回键 :linefeed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10431667/