gpt4 book ai didi

objective-c - UITextView,如何删除返回键 :linefeed?

转载 作者:行者123 更新时间:2023-11-28 17:58:53 25 4
gpt4 key购买 nike

我想删除按回车键输入的换行符。但是当我执行以下操作时,它会从文本中删除最后一个字符。为什么?

- (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com