UITextView如何设置提示性文字

来源:互联网 发布:python 2.7 3.5 区别 编辑:程序博客网 时间:2024/06/16 04:42

给UITextView设置提示性文字,输入时提示性文字消失。结束输入时,若输入内容为空,则提示性文字再次出现。

例如:提示性文字为“随便说点什么吧”;

#define TextViewDefaultText @"随便说点什么吧"self.textView.text = TextViewDefaultText;self.textView.textColor = [UIColor lightGrayColor];

在UITextViewDelegate方法中实现:

#pragma mark --------TextViewDelegate-------(BOOL)textViewShouldBeginEditing:(UITextView *)textView{    if ([textView.text isEqualToString:TextViewDefaultText]) {        textView.text = @"";    }    textView.textColor = [UIColor blackColor];    return YES;}-(BOOL)textViewShouldEndEditing:(UITextView *)textView{    NSString *str = [textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];    if ([str isEqualToString:@""]) {        textView.text = TextViewDefaultText;        textView.textColor = [UIColor lightGrayColor];    }    return YES;}

这样就OK了!


0 0