如何在 UITextView (iphone) 中执行此操作
在我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以用来实现此概念的所有委托方法都可以在 UITextViewDelegate 协议参考。
All the delegate methods you can use to implement this concept can be found in the UITextViewDelegate Protocol Reference.
您需要使用所需的文本在文本视图顶部覆盖 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.