UITextView :文本从文本视图的第二行开始

发布于 2024-12-14 00:15:05 字数 1987 浏览 2 评论 0原文

我有一个奇怪的问题。我有 3 个 UITextField 和 1 个 UITextView。我可以通过以下方式从一个 UITextField 移动到下一个 UITextField

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    NSInteger nextTag = textField.tag + 1;
    //-- try to find next responde
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

    if (nextResponder) 
    {
        //-- found next responce ,so set it
        [nextResponder becomeFirstResponder];
    }
    else
    {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        //-- not found remove keyboard
        [textField resignFirstResponder];
        return YES;
    }   

    return YES;
}

一切正常,直到到达 UITexView,然后我的光标移动到第二行文本视图代替文本视图的第一行。

文本视图取自IB。

文本视图的代码:

- (void) textViewDidBeginEditing:(UITextView *)textView
{
    optionalText.hidden = YES ;
    [scrollView setContentOffset:CGPointMake(0, textView.center.y-100) animated:YES];
}

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{       
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"])
    {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

        // Return FALSE so that the final '\n' character doesn't get added      
        if ([[commentTxtView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
        {
            optionalText.hidden = NO ;
            NSLog(@"Text View is null");
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }

        return FALSE;
    }

    return TRUE;
}

看起来像这样:

在此处输入图像描述

可能是什么问题?为什么它不从文本视图的第一行开始?

I have a strange problem. I have 3 UITextFields and one UITextView. I can move from one UITextFieldto next UITextField by:

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    NSInteger nextTag = textField.tag + 1;
    //-- try to find next responde
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

    if (nextResponder) 
    {
        //-- found next responce ,so set it
        [nextResponder becomeFirstResponder];
    }
    else
    {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        //-- not found remove keyboard
        [textField resignFirstResponder];
        return YES;
    }   

    return YES;
}

Its all works fine until it comes to UITexView, then my cursor moves at 2nd line of the text view in place of 1st line of text view.

Text view is taken from IB.

The code for text view:

- (void) textViewDidBeginEditing:(UITextView *)textView
{
    optionalText.hidden = YES ;
    [scrollView setContentOffset:CGPointMake(0, textView.center.y-100) animated:YES];
}

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{       
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"])
    {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

        // Return FALSE so that the final '\n' character doesn't get added      
        if ([[commentTxtView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
        {
            optionalText.hidden = NO ;
            NSLog(@"Text View is null");
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }

        return FALSE;
    }

    return TRUE;
}

It looks like this:

enter image description here

What could be the problem? Why does it not start from 1st line of text view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2024-12-21 00:15:05

我敢打赌,如果 nextResponder 是您的 UITextField,您的 textFieldShouldReturn: 应该返回 NO。现在您总是返回 YES,这意味着密钥可能会传递给 UITextView,然后插入新行。

I bet your textFieldShouldReturn: should return NO in case the nextResponder is your UITextField. Right now you are always returning YES, which means that the key probably being passed to the UITextView, which will then insert the new line.

左耳近心 2024-12-21 00:15:05

对于任何寻求 Swift 2.0 翻译的人...

yourUIITextView.becomeFirstResponder() 之后,不要在 textFieldShouldReturn 中返回 true,而是返回

For Anybody coming in looking for Swift 2.0 translation...

Instead of returning true in textFieldShouldReturn after yourUIITextView.becomeFirstResponder(), return false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文