控件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. 修正ios h5上传图时的图片方向问题

     .ios上传会在exif中带一个 Orientation的属性,这个属性在windows中不会生效,在ios浏览器中会生效,造成图片在windows资源管理器中与ios浏览器中方向不一致  为了用户 ...

  2. Android_开发工具的下载和开发环境的搭建

    一.Android开发工具的下载  Android开发者官网: http://developer.android.com/           Android开发工具:http://www.andro ...

  3. jQuery中$.post()的使用

    $.post()方法是jquery本身对ajax的一个简单封装,其效果等价于: $.ajax({ url:url, type:"POST", data:data, dataType ...

  4. Linux大文件分割split和合并cat使用方法

    本文主要介绍linux下两个命令:split和cat.其中,相信大家都熟悉cat命令,一般用来查看一个文件的内容,但是它还其它的功能,比如这里要介绍的文件合并功能,它可把多个文件内容合并到一个文件中. ...

  5. Machine Learning - 第4周(Neural Networks: Representation)

    Neural networks is a model inspired by how the brain works. It is widely used today in many applicat ...

  6. Tiny PXE Server简介

    Tiny PXE Server简介Tiny PXE Server是一款小巧而功能强大的网启软件.支持DHCP TFTP HTTP BINL DNS等多个协议,支持grub4dos,pxelinux,i ...

  7. signalR的一些细节

    获取根目录通过AppDomain.CurrentDomain.BaseDirectory 因为不能直接获取session ,使用的替代方案如下 private static Dictionary< ...

  8. linux笔记:linux常用命令-文件搜索命令

    文件搜索命令:find(文件搜索) 一些示例: 注意:在以文件名为条件进行搜索时,支持通配符. 多条件搜索,以及直接对搜索到的文件进行操作: 文件搜索命令:locate(在文件资料库中查找文件) 文件 ...

  9. 5.5 Selenium2中的元素定位

    WebDriver的更加面向对象的方式大大降低了Selenium的入门门槛,对Web元素的操作也非常之简单易学.实际项目用起来,工作量最大的部分就是你如何解析定位到你的目标项目页面中的各种元素.好比你 ...

  10. NSInternalInconsistencyException: loaded the "XXXView" nib but the view outlet was not set

    运行过程中App崩溃,报错如标题. 原因: viewController的view没有绑定xib的view. 解决方法: 在File's Owner中将view与viewController的view ...