iOS设置textView的placeholder
转载:http://blog.sina.com.cn/s/blog_7a1b23430102wkys.html
#import "ViewController.h"
@interface ViewController ()<</span>UITextViewDelegate>
{
UILabel *textViewPlaceholderLabel;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
1、在UITextView上加上一个UILabel
textViewPlaceholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(53, 202, 150, 25)];
textViewPlaceholderLabel.text = @"请输入你的内容";
textViewPlaceholderLabel.textColor = [UIColor grayColor];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 200, 150, 250)];
textView.delegate = self;
textView.backgroundColor = [UIColor clearColor];
textView.layer.borderWidth = 1.0f;
textView.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: textViewPlaceholderLabel];
[self.view addSubview: textView];
}
//设置textView的placeholder
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//[text isEqualToString:@""] 表示输入的是退格键
if (![text isEqualToString:@""])
{
textViewPlaceholderLabel.hidden = YES;
}
//range.location == 0 && range.length == 1 表示输入的是第一个字符
if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
{
textViewPlaceholderLabel.hidden = NO;
}
return YES;
}
最新文章
- 算术表达式解析(第二版) C++11版
- Android深度探索--HAL与驱动开发----第六章读书笔记
- Rubinius 2.0 发布,Ruby 虚拟机
- Java基础-关于session的详细解释
- Spring.Net+Nhibernate
- c++函数重载---2
- Android 通过xml 自定义图片
- Helpers\TableBuilder
- BZOJ 1710: [Usaco2007 Open]Cheappal 廉价回文
- build opencv with python support
- wpf中数据绑定(Datacontext)的应用
- CI集成phpunit Error: No code coverage driver is available 的解决
- 学习Lucene、solr之前应当了解的一些术语
- webService(一)开篇
- ionic调用手机系统的拨打电话
- hiho 第六周 01背包
- jsp-9大内置对象简介
- Apktool(1)——Apktool的安装
- day7--面向对象进阶(内含反射和item系列)
- LDA线性判别分析(转)
热门文章
- 第十三天笔记Cookie
- Xcode 12.x下载了iOS10.x模拟器无法创建对应Device问题修复
- 可收集ALC问题[Microsoft.Data.SqlClient is not supported on this platform.]
- [转]有限状态机FSM(finite state machine) 一
- py10函数之嵌套-名称空间作用域
- Linux FTP服务器配置文件vsftpd.conf 配置
- Java8 提供CompletableFuture来简化高并发异步处理编程
- SY有了不适症状吃抗过敏药试试
- unity3d Time.deltaTime个人理解
- 像MIUI一样做Zabbix二次开发(2)——Zabbix与商业及开源产品的比较分析