转载: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;

}

最新文章

  1. 算术表达式解析(第二版) C++11版
  2. Android深度探索--HAL与驱动开发----第六章读书笔记
  3. Rubinius 2.0 发布,Ruby 虚拟机
  4. Java基础-关于session的详细解释
  5. Spring.Net+Nhibernate
  6. c++函数重载---2
  7. Android 通过xml 自定义图片
  8. Helpers\TableBuilder
  9. BZOJ 1710: [Usaco2007 Open]Cheappal 廉价回文
  10. build opencv with python support
  11. wpf中数据绑定(Datacontext)的应用
  12. CI集成phpunit Error: No code coverage driver is available 的解决
  13. 学习Lucene、solr之前应当了解的一些术语
  14. webService(一)开篇
  15. ionic调用手机系统的拨打电话
  16. hiho 第六周 01背包
  17. jsp-9大内置对象简介
  18. Apktool(1)——Apktool的安装
  19. day7--面向对象进阶(内含反射和item系列)
  20. LDA线性判别分析(转)

热门文章

  1. 第十三天笔记Cookie
  2. Xcode 12.x下载了iOS10.x模拟器无法创建对应Device问题修复
  3. 可收集ALC问题[Microsoft.Data.SqlClient is not supported on this platform.]
  4. [转]有限状态机FSM(finite state machine) 一
  5. py10函数之嵌套-名称空间作用域
  6. Linux FTP服务器配置文件vsftpd.conf 配置
  7. Java8 提供CompletableFuture来简化高并发异步处理编程
  8. SY有了不适症状吃抗过敏药试试
  9. unity3d Time.deltaTime个人理解
  10. 像MIUI一样做Zabbix二次开发(2)——Zabbix与商业及开源产品的比较分析