控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行输入。2.UITextField有placeholder属性,而UITextView没有。至于两者的代理方法,原理基本差不多,只是方法名略有差异。

实现该功能有两种方式 一种是 ①使用通知 显示隐藏遮盖物

②使用代理 给文本框重新赋值

1.在创建textView的时候,赋值其文本属性

即textView.text = @"想说的话";

2.在开始编辑和结束编辑的代理方法中进行判断

 //
// ViewController.m
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "ViewController.h"
#import "DJTextView.h"
#import "DJplaceHolderTextView.h"
@interface ViewController () <UITextViewDelegate>
@property (nonatomic,strong)DJplaceHolderTextView *placeHolderText;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
text1.backgroundColor = [UIColor grayColor];
text1.placeholder = @"请输入账号";
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:text1]; // 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
DJTextView *text = [[DJTextView alloc]initWithFrame:CGRectMake(, , , )];
text.backgroundColor = [UIColor grayColor];
text.placeholder = @"请输入账号";
text.placeholderColor = [UIColor whiteColor];
[self.view addSubview:text]; // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
_placeHolderText = [[DJplaceHolderTextView alloc]initWithFrame:CGRectMake(, , , )];
_placeHolderText.backgroundColor = [UIColor grayColor];
_placeHolderText.placeholder = @"请输入账号";
_placeHolderText.text = _placeHolderText.placeholder;
_placeHolderText.delegate = self;
[self.view addSubview:_placeHolderText];
}
/**
* 开始编辑事件
*/
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder);
if ([textView.text isEqualToString:self.placeHolderText.placeholder]) {
textView.text = @"";
}
}
/**
* 结束编辑事件
*/
- (void)textViewDidEndEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder);
if (textView.text.length < ) {
textView.text = self.placeHolderText.placeholder;
}
} @end
 // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
 //
// DJplaceHolderTextView.h
// 绘制TextView
//
// Created by zjj on 15/7/2.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import <UIKit/UIKit.h> @interface DJplaceHolderTextView : UITextView
/**
* placehold 用户输入前文本提示
*/
@property (nonatomic,copy) NSString *placeholder;
@end
// 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
 //
// DJTextView.h
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import <UIKit/UIKit.h>
/**
* UITextView 实现 placeholder 及隐藏键盘
*/
@interface DJTextView : UITextView
/**
* placehold 用户输入前文本提示
*/
@property (nonatomic,copy) NSString *placeholder;
/**
* placeholder 文字颜色
*/
@property (nonatomic,strong)UIColor *placeholderColor;
/**
* placeholder 提示label
*/
@property (nonatomic,strong)UILabel *placeholderLabel;
/**
* 文本改变事件
*/
-(void)textChanged:(NSNotification*)notification;
@end
 //
// DJTextView.m
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "DJTextView.h" @implementation DJTextView //-(void)setPlaceholder:(NSString *)placeholder
//{
// _placeholder = placeholder;
//
// [self setNeedsDisplay];
//}
//
//- (void)drawRect:(CGRect)rect
//{
//
// [self.placeholder drawInRect:rect withAttributes:nil];
//} - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; // placeHolderLabel = nil;
//
// [placeholderColor release]; placeholderColor = nil;
//
// [placeholder release]; placeholder = nil; // [super dealloc]; } - (void)awakeFromNib { [super awakeFromNib]; [self setPlaceholder:@""]; [self setPlaceholderColor:[UIColor lightGrayColor]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } - (id)initWithFrame:(CGRect)frame { if( (self = [super initWithFrame:frame]) ) { [self setPlaceholder:@""]; [self setPlaceholderColor:[UIColor lightGrayColor]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; } return self; } - (void)textChanged:(NSNotification *)notification { if([[self placeholder] length] == ) { return; } if([[self text] length] == ) { [[self viewWithTag:] setAlpha:]; } else { [[self viewWithTag:] setAlpha:]; } } - (void)setText:(NSString *)text { [super setText:text]; [self textChanged:nil]; } - (void)drawRect:(CGRect)rect { if( [[self placeholder] length] > ) { if ( self.placeholderLabel == nil ) { self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(,,self.bounds.size.width - ,)];
self.placeholderLabel.lineBreakMode = UILineBreakModeWordWrap;//过时的
self.placeholderLabel.numberOfLines = ; self.placeholderLabel.font = self.font; self.placeholderLabel.backgroundColor = [UIColor clearColor]; self.placeholderLabel.textColor = self.placeholderColor; self.placeholderLabel.alpha = ; self.placeholderLabel.tag = ; [self addSubview:self.placeholderLabel]; } self.placeholderLabel.text = self.placeholder; [self.placeholderLabel sizeToFit]; [self sendSubviewToBack:self.placeholderLabel]; } if( [[self text] length] == && [[self placeholder] length] > ) { [[self viewWithTag:] setAlpha:]; } [super drawRect:rect]; }
////隐藏键盘,实现UITextViewDelegate
//
//-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
//
//{
//
// if ([text isEqualToString:@"\n"]) {
//
// [textView resignFirstResponder];
//
// return NO;
//
// }
//
// return YES;
//
//} @end

推荐使用第一种

UITextView添加一个placeholder功能的更多相关文章

  1. 为Pythonic论坛添加一个“专题”功能(续)

    上篇博文<为Pythonic论坛添加一个“专题”功能>,在模板的层次上对发帖进行了限制.也就是根据用户是否拥有权限来决定是否显示发帖框. 但是自从这么“投机取巧”的写完模板后,整夜辗转反侧 ...

  2. iOS 给UITextView加一个placeholder

    苹果并没有为UITextView提供placeholder功能.我们可以通过两种办法实现. 方法一: 思路:设置默认显示的文字,颜色设置为灰色.代理方法监听textView点击. 缺点:如果点击到文字 ...

  3. 为Pythonic论坛添加一个“专题”功能

    代码还没读完就踏上了修改功能的深坑.还好思路清晰,通过修改模板和视图,实现了专题模块 原论坛的模式是用户点击节点发帖,然后就归到节点的分类里面了.我需要一个功能,就是右侧需要一个专题区,管理员发帖的话 ...

  4. 添加一个Android框架层的系统服务与实现服务的回调

    2017-10-09 概述 所谓Android系统服务其本质就是一个通过AIDL跨进程通信的小Demo的延伸而已.按照 AIDL 跨进程通信的标准创建一套程序,将服务端通过系统进程来运行实现永驻内存, ...

  5. 调用Android自带日历功能(日历列表单、添加一个日历事件)

    调用Android自带日历功能  觉得这篇文章不错,转载过来. 转载:http://blog.csdn.net/djy1992/article/details/9948393 Android手机配备有 ...

  6. jQuery插件之路(一)——试着给jQuery的一个Carousel插件添加新的功能

    前几日在网上看到了一个关于Carousel插件的教学视频,于是也顺便跟着学习着做了一下.但是在做完之后发现,在别的网站上面看到类似的效果要比现在做的这个要多一个功能,也就是在底下会有一些按钮,当鼠标放 ...

  7. 为 JS 的字符串,添加一个 format 的功能。

    <script> String.prototype.format = function (kwargs) { var ret = this.replace(/\{(\w+)\}/g, fu ...

  8. Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql

    在上一篇博客中介绍怎样在SAE平台搭建一个html5的电子请帖网站,收到很多反馈,也有很多人送上婚礼的祝福,十分感谢! web开发从零学起,记录自己学习过程,各种前端大神们可以绕道不要围观啦 大婚将至 ...

  9. Hexo next博客添加折叠块功能添加折叠代码块

    前言 有大段的东西想要放上去,但又不想占据大量的位置.折叠是最好的选择.下面在Hexo的主题上定制添加折叠功能. 本文基于Hexo Next的主题修改.其他主题应该也差不多. 在main.js中添加折 ...

随机推荐

  1. PHP 用文件流方式展示图片

    public function index(){ $img = 'http://img.pf.loc/static/images/2016/05/24/21d98edf98bd6c30afe1c838 ...

  2. Web服务器异常问题记录

    1.使用命令,出现"-bash: 命令: Input/output error" 重启服务器后正常,网上查了下是说硬盘写入读取异常,经过和服务器厂商沟通后,确认是硬件问题导致(硬盘 ...

  3. Android控件之RadioGroup与RadioButton(单选控件)

    一.RadioGroup与RadioButton 1.什么是RadioGroup: RadioButton的一个集合,提供多选机制 2.什么是RadioButton: RadioButton包裹在Ra ...

  4. hiho_1057_performance_log

    题目大意 给出一个函数调用日志,判断日志是否合法,且求出合法日志中函数调用的时间长度. 题目链接:performance log 题目分析 首先需要清除非法日志的几种情形: (1)日志的时间戳不是按照 ...

  5. php 注入

    SELECT * FROM `users` WHERE name = 'a\'b\'d' LIMIT 0 , 30 这个是有结果的,运行正确的,和一般想的不一样,单引号里面可以套单引号,只要里面的单引 ...

  6. ADO

    目 录 第1章 基础    1 1.1 引入ADO库文件    1 1.1.1 版本    1 1.2 初始化OLE/COM库环境    2 1.3 comdef.h    2 1.3.1 字符串编码 ...

  7. 设置segue跳转页面

    第二种是利用ViewController与ViewController之间,拖拽添加segue 方法中提到的设置segue的identifier界面 在.h文件中声明 - (IBAction)goto ...

  8. js字符串函数之indexOf()

    indexOf 返回字符串中指定字符首次出现的位置 var str="hello, I am Miss bean!"; str.indexOf("l")//结果 ...

  9. ubuntu 系统出错一览

    1.系统升级出错:打开终端输入:sudo apt-get install -f

  10. (11)odoo权限机制

    -----------------更新时间:10:21 2016-09-29 星期四14:31 2016-09-28 星期三 权限对象命名修改18:06 2016-09-18 星期日11:55 201 ...