iphone - UITextView : Text starts from 2nd line of text view

标签 iphone uitextview

我有一个奇怪的问题。我有 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,然后我的光标移动到 TextView 的第二行,而不是 TextView 的第一行。

TextView 取自IB。

TextView 的代码:

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

看起来像这样:

enter image description here

可能是什么问题?为什么它不从 TextView 的第一行开始?

最佳答案

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

关于iphone - UITextView : Text starts from 2nd line of text view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8037679/

相关文章:

ios - 音频和视频流播放器

iphone - 项目无法在苹果设备上运行

ios - UILabel动态获取字体大小 - swift 3

ios - 滚动出屏幕时 UITableViewCell 中 UITextView 的 ResignFirstResponder

objective-c - 在 UITextView 中只允许字母数字字符

iphone - ios刮刮卡效果崩溃

iphone - 访问 iPhone 上的音乐库和播放列表

ios - 在ipad上隐藏状态栏

ios - 如何将 UITextView 放在 TableViewController 的底部?

xcode - 如何在 UITextView 返回/搜索键上添加一个 Action ?(swift 2)