我想要删除返回键输入的行提要。但是当我做以下操作时,它会从文本中删除最后一个字符。为什么?
- (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 ">>"发布于 2012-05-03 12:50:09
我想你可以做这样的事,
- (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;
}发布于 2012-05-03 12:51:49
或者在读完字符串之后,把它放到子字符串中:
string = [string stringByReplacingOccurrencesOfString:@"\n;" withString:@""];发布于 2012-05-03 13:03:10
shouldChangeTextInRange是UITextViewDelegate的一部分,在之前称为--在textView中更改新文本。因此,您可以这样做:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if ([text isEqualToString:@"\n"])
{
return NO;
}
return YES;
}https://stackoverflow.com/questions/10431667
复制相似问题