用UITextView模拟UITextField的placeHolder

效果:

源码:

//
// ViewController.m
// TextView
//
// Created by YouXianMing on 14/12/18.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h" static NSString *placeHolderStr = @"User Name"; @interface ViewController ()<UITextViewDelegate> @property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *button; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 反应的按钮
self.button = [[UIButton alloc] initWithFrame:self.view.bounds];
[self.button addTarget:self
action:@selector(buttonEvent)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button]; // textView
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(, , , )];
self.textView.layer.borderWidth = .f;
self.textView.layer.borderColor = [UIColor grayColor].CGColor;
self.textView.delegate = self;
self.textView.text = placeHolderStr;
self.textView.font = [UIFont systemFontOfSize:.f];
self.textView.textColor = [UIColor grayColor];
[self.view addSubview:self.textView];
} #pragma mark - 代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// 设置编辑状态文字颜色
textView.textColor = [UIColor blackColor]; // 如果文字为placeHolder文字
if ([textView.text isEqualToString:placeHolderStr]) {
textView.text = @"";
} return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView { // 如果长度为0,则显示placeHolder文字
if (textView.text.length == ) {
textView.text = placeHolderStr;
textView.textColor = [UIColor grayColor];
} return YES;
} /**
* 反应的按钮
*/
- (void)buttonEvent {
[self.textView resignFirstResponder];
} @end

核心代码:

最新文章

  1. CSS3 选择器——基本选择器
  2. &lt;HTML5和CSS3响应式WEB设计指南&gt;译者序
  3. Python学习路程day10
  4. Jenkins 笔记
  5. asp.net 查询,导出
  6. poj2823Sliding Window(线段树求最值)
  7. ie版本判断的js hack
  8. 使用Teleport Pro离线下载网页所有内容
  9. 初识Spider_Man(爬爬虫)
  10. IOS开发之XCode学习010:定时器和视图对象
  11. Java基本语法-----java数据类型的转换
  12. Log4j2 设置控制台打印彩色日志
  13. 定时器setTimeout()和Node.js的Event Loop
  14. MySQL必知必会第十一章-
  15. (最小生成树 并查集)P1111 修复公路 洛谷
  16. Codeforces 109D String Transformation 字符串 哈希 KMP
  17. python3 day02 大纲
  18. 【CSS3】响应式布局
  19. Python3+Selenium环境配置
  20. 在Android平台下搭建PhoneGap开发环境--用HTML5开发游戏

热门文章

  1. Linux top命令用法
  2. C#中的Dictionary类,默认key是区分大小写的
  3. java中删除list指定元素遇到的问题
  4. 剑指offer65:矩阵中的路径
  5. JDK 泛型
  6. Ruby on Rails中的Rake教程(Rake如何把我灌醉!)
  7. C# 在同一个项目里启动不同的类文件
  8. @Html.Raw()用法和Html.ActionLink的用法总结
  9. day_02mysql表的约束设计
  10. 为什么说 LINQ 要胜过 SQL