UITextView实现placeholder的猥琐做法
2015-08-07 16:08
399 查看
我们都知道iOS开发中的
如何让
把
在开始编辑的代理方法里清除
在结束编辑的代理方法里在设置
初始化
别忘了设置
在开始编辑的代理方法里面,判断如果是
在结束编辑的代理方法里,判断如果
至此,就很猥琐的实现了
我看了网上一些实现
UITextField有个
placeholder属性,
placeholder可以很方便引导用户输入。但是
UITextView却没有
placeholder属性
猥琐法分析
如何让UITextView也有
placeholder功能呢?今天给各位分享一个比较猥琐的做法。思路大概是这样的:
把
UITextView的
text当
placeholder使用。
在开始编辑的代理方法里清除
placeholder。
在结束编辑的代理方法里在设置
placeholder。
实现方法
创建UITextView
UITextView *textViewPlaceholder = [[UITextView alloc] initWithFrame:CGRectMake(20, 70, SCREEN.width - 40, 100)]; textViewPlaceholder.backgroundColor = [UIColor whiteColor]; textViewPlaceholder.text = @"关注微信公众号iOS开发:iOSDevTip"; textViewPlaceholder.textColor = [UIColor grayColor]; textViewPlaceholder.delegate = self; [self.view addSubview:textViewPlaceholder];
初始化
UITextView,给
UITextView的
text赋值,并且给
UITextView的
textColor属性设置成灰色,让其看起来更像
placeholder。
别忘了设置
UITextView的代理,因为后面我们要用到
UITextView的两个代理方法。
开始编辑的代理方法
- (void)textViewDidBeginEditing:(UITextView *)textView { if ([textView.text isEqualToString:@"关注微信公众号iOS开发:iOSDevTip"]) { textView.text = @""; textView.textColor = [UIColor blackColor]; } }
在开始编辑的代理方法里面,判断如果是
UITextView的
text的值是
placeholder,那么,就清空
text,并且把
textColor设置成真正的内容颜色,假设是黑色。
结束编辑的代理方法
- (void)textViewDidEndEditing:(UITextView *)textView { if (textView.text.length<1) { textView.text = @"关注微信公众号iOS开发:iOSDevTip"; textView.textColor = [UIColor grayColor]; } }
在结束编辑的代理方法里,判断如果
UITextView的
text值为空,那么,就要把需要设置的
placeholder赋值给
UITextView的
text,并且将
textColor属性设置成灰色。
添加轻击手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; tapGesture.numberOfTapsRequired = 1; //点击次数 tapGesture.numberOfTouchesRequired = 1; //点击手指数 [self.view addGestureRecognizer:tapGesture]; //轻击手势触发方法 -(void)tapGesture:(UITapGestureRecognizer *)sender { [self.view endEditing:YES]; }
至此,就很猥琐的实现了
placeholder功能。为了方便测试,我加了一个手势。作用是用键盘消失,这样可以测试结束编辑的时候
placeholder会不会显示。
我看了网上一些实现
UITextView的
placeholder功能的更猥琐的做法,在
UITextView上面盖一个
UILabel,然后在
UITextView的代理方法里控制
UILabel的值。还有用两个
UITextView实现的。
相关文章推荐
- Android UI自动化测试最佳实
- iOS UITableViewCell重用问题
- Android中的Handler, Looper, MessageQueue和Thread
- Team Queue(POJ 2259)
- 设置HttpSendRequest阻塞时间
- (四) Basic Build Customization(基本的构建定制 :签名,构建,混淆)
- A1062 Talent and Virtue (25)
- requireJS使用
- (绝对有用)iOS获取UUID,并使用keychain存储
- ueditor在QQ浏览器或者IE浏览器中无法加载
- ueditor在QQ浏览器或者IE浏览器中无法加载
- UICollectionView的使用
- 双飞翼布局介绍-始于淘宝UED
- Field 'id' doesn't have a default value
- UIView添加动画
- IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
- 说说SAP Business One怎么样
- UIScrollView实现图片浏览
- 造成ORA-01843 无效的月份 的一些原因 1) 当我们在一个中文环境的客户端使用如下sql语句 INSERT INTO "temptable" ( DELIVER_DATE ) VALUES
- (三) Build Task(构建任务)