0

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

- (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 ">>"
4

6 回答 6

4

我认为你可以做这样的事情,

    - (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-03T12:50:09.380 回答
4

或者也许在您阅读字符串之后并将其放入某个子字符串:

string = [string stringByReplacingOccurrencesOfString:@"\n;" withString:@""];
于 2012-05-03T12:51:49.120 回答
3

shouldChangeTextInRange 是UITextViewDelegate的一部分,在 textView中更改新文本之前调用。因此,您可以这样做:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if ([text isEqualToString:@"\n"])
    {
        return NO;
    }

    return YES;
}
于 2012-05-03T13:03:10.003 回答
2

使用它以获得更好的解决方案,因为它不允许用户在任何情况下发布任何空白消息。

//These for loops will remove the spaces and new line characters from start and end of the string

    //&&//

    NSMutableString *temp = [[NSMutableString alloc] initWithString:posttextview.text];

    //Remove spaces and new line characters from start
    for(int i = 0; i < yourtext.length; i++)
    {
        NSString *temp1 = [NSMutableString stringWithString:[temp substringWithRange:NSMakeRange(0,1)]];

        if([temp1 isEqualToString:@"\n"] || [temp1 isEqualToString:@" "])
        {
            [temp deleteCharactersInRange:NSMakeRange(0,1)];
        }

        else
        {
            break;
        }
    }

    yourtext.text = temp;

    //Remove spaces and new line characters from end
    for(int i = 0; i < yourtext.length; i++)
    {
        NSString *temp1 = [NSMutableString stringWithString:[temp substringWithRange:NSMakeRange(posttextview.text.length - 1,1)]];

        if([temp1 isEqualToString:@"\n"] || [temp1 isEqualToString:@" "])
        {
            [temp deleteCharactersInRange:NSMakeRange(posttextview.text.length - 1,1)];

            yourtext.text = temp;
        }

        else
        {
            break;
        }
    }

    yourtext.text = temp;

    //**//
于 2014-08-27T09:22:20.320 回答
1

问题是调用 shouldChangeCharactersImRange 的时间,新文本实际上还没有改变(这就是为什么它没有命名为 didChangeCharactersInRange ...)。因此,如果您遇到换行符,请不要欺骗子字符串,只需存储/处理文本视图到目前为止包含的字符串,然后返回 NO。

于 2012-05-03T12:52:38.743 回答
0

首先在 .h 文件中添加 UITextViewDelegate

@interface YourClass : UITextField <UITextFieldDelegate> {
}

然后实现委托方法

-(BOOL)shouldChangeCharactersInRange:replacementString:
于 2012-05-03T13:19:50.620 回答