我使用了两种delegate
方法为textView创建占位符。
代码:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if([Description.text isEqualToString:@"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
{
Description.text=nil;
}
if([Display.text isEqualToString:@"Display"]&&[Display.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
{
Display.text=nil;
}
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if ([Description.text isEqualToString:@""]) // Description is textViewObject
{
Description.text=@"Description of home";
Description.font=[UIFont fontWithName:@"Helvetica" size:18];
}
if ([Display.text isEqualToString:@""])
{
Display.text=@"Display"; // Display is textViewObject
Display.font=[UIFont fontWithName:@"Helvetica" size:18];
}
return YES;
}
@end
这是正确的。但是问题是当我点击任何textView时,文本从所有textView获得nil
。这是因为我已将所有textView连接到委托。
我希望该文本仅在特定的textView上消失。
答案 0 :(得分:2)
您可以在委托方法中添加条件,如下所示:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == yourTextView1)
{
//your code for perticular textView1
}
else if(textView == yourTextView2)
{
//your code for perticular textView2
}
return YES;
}
答案 1 :(得分:0)
您需要以某种方式区分每个UITextViews
。
如果您要在屏幕上显示少量UITextViews
,则应将每个UItextView保存为属性。在creating IBOutlets
然后按顺序命名UITextViews
:textView1,textView2等......
然后在您的委托方法中,您可以检查方法中传递的textView是否等于要从中删除文本的属性。
if (textView == self.textView1) {
//delete text
} else {
//do nothing or something else
}
答案 2 :(得分:-1)
检查代表是否在想要的UITextView
上运行:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == Description && [Description.text isEqualToString:@"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
{
Description.text=nil;
}
if(textView==Display && [Display.text isEqualToString:@"Display"]&&[Display.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
{
Display.text=nil;
}
return YES;
}
答案 3 :(得分:-1)
@最简单的解决方案 这个问题的CmKndy是https://stackoverflow.com/a/10201671/3633534
在您的班级中使用UITextViewDelegate
委托,并在@"placeholder text here..."
中添加textView
文字,lightGrayColor
默认显示。
以下代码适用于textView
中的占位符:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"placeholder text here..."]) {
textView.text = @"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"placeholder text here...";
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
针对您的问题[更新]
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == Description && [Description.text isEqualToString:@"Description of home"])
{
Description.text = @"";
}
else if(textView == Display && [Display.text isEqualToString:@"Display"])
{
Display.text = @"";
}
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if (textView == Description && [Description.text isEqualToString:@""]) // Description is textViewObject
{
Description.text = @"Description of home";
Description.font=[UIFont fontWithName:@"Helvetica" size:18];
}
else if (textView == Display && [Display.text isEqualToString:@""])
{
Display.text = @"Display"; // Display is textViewObject
Display.font=[UIFont fontWithName:@"Helvetica" size:18];
}
return YES;
}
@end