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

标签 objective-c ios cocoa-touch uikit uitextview

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

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

相关文章:

ios - 我在创建一个单例吗?

objective-c - 生成文件问题。 [Ubunt、GNUstep、ObjC、]

ios - 在 RubyMotion 中,如何在触摸后退链接时取消列表刷新?

objective-c - Objective-C 中的 Nil 对象引用

ios - 如何在 iOS 上以编程方式创建 .txt 文件

ios - 关闭 UIImagePickerController 将 UITabBarController 重置为 0 索引 - iOS

ios - iOS 上没有 UIActivityViewController 的 Facebook、twitter 和邮件共享

ios - 检查NSString是否为非HTML上的HTML字符串?

ios - 获取另一个 UIView 下的 UIView

ios - 自定义 Push Segue 移除 Storyboard中的导航栏和标签栏