UITextView:文本从文本视图的第二行开始 [英] UITextView : Text starts from 2nd line of text view

查看:102
本文介绍了UITextView:文本从文本视图的第二行开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

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

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.

文本视图取自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;
}

看起来像这样:

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

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

推荐答案

我打赌你的 textFieldShouldReturn: nextResponder 是您的 UITextField 应返回 C $ C>。现在你总是返回 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.

这篇关于UITextView:文本从文本视图的第二行开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆