如何在 UITextView (iphone) 中执行此操作

发布于 2024-12-06 23:36:04 字数 1086 浏览 0 评论 0原文

在我的textView中有一个默认值“名字”

我想在其中实现这样的操作..

1)它应该首先显示“名字”
2)当我开始编辑时它应该变成nil
3)当我完成编辑后,如果该字段仍然为零,那么它应该再次显示为“名字”,

有人可以帮助我吗?

//*这就是我现在正在做的事情**//

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
    replacementText:(NSString *)text
         {    
if (first_name.text=@"") {
    first_name.text=@"First Name(Required)";
    }
// 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];

    // Return FALSE so that the final '\n' character doesn't get added
    return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
   return TRUE;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(@"textViewShouldBeginEditing");
textView.scrollEnabled=NO;

if (textView == first_name) {
    first_name.text=nil;
}


return YES;

}

In My textView there is a default value of "First name "

I want to implement such operation in it..

1) it should be shown "first name" firstly
2) when I start editing it should became nil
3)when I am complete with my editing if the field is still nil then it should be shown as "first name" again

can anyone help me with this?

//*This is now what I am doing now**//

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
    replacementText:(NSString *)text
         {    
if (first_name.text=@"") {
    first_name.text=@"First Name(Required)";
    }
// 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];

    // Return FALSE so that the final '\n' character doesn't get added
    return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
   return TRUE;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(@"textViewShouldBeginEditing");
textView.scrollEnabled=NO;

if (textView == first_name) {
    first_name.text=nil;
}


return YES;

}

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

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

发布评论

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

评论(2

蓝礼 2024-12-13 23:36:04

您可以用来实现此概念的所有委托方法都可以在 UITextViewDelegate 协议参考

All the delegate methods you can use to implement this concept can be found in the UITextViewDelegate Protocol Reference.

风吹过旳痕迹 2024-12-13 23:36:04

您需要使用所需的文本在文本视图顶部覆盖 UILabel,将其 userInteractionEnabled 设置为 NO,并在文本字段委托方法 shouldBeginEditing 上隐藏 UILabel,然后返回 YES 并在 shouldFinishEditing 上取消隐藏它。

You need to overlay a UILabel on top of the text view with the text you want, set its userInteractionEnabled to NO and on the text field delegate method shouldBeginEditing you hide the UILabel before returning YES and unhide it on shouldFinishEditing.

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