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
,然后我的光标移动到第二行文本视图代替文本视图的第一行。
文本视图取自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 UITextField
s and one UITextView
. I can move from one UITextField
to 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:
What could be the problem? Why does it not start from 1st line of text view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢打赌,如果
nextResponder
是您的UITextField
,您的textFieldShouldReturn:
应该返回NO
。现在您总是返回YES
,这意味着密钥可能会传递给UITextView
,然后插入新行。I bet your
textFieldShouldReturn:
should returnNO
in case thenextResponder
is yourUITextField
. Right now you are always returningYES
, which means that the key probably being passed to theUITextView
, which will then insert the new line.对于任何寻求 Swift 2.0 翻译的人...
在
yourUIITextView.becomeFirstResponder()
之后,不要在textFieldShouldReturn
中返回true
,而是返回假
。For Anybody coming in looking for Swift 2.0 translation...
Instead of returning
true
intextFieldShouldReturn
afteryourUIITextView.becomeFirstResponder()
, returnfalse
.