控件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. centos JDK安装

    第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...

  2. Canu Quick Start(快速使用Canu)

    Canu Quick Start Canu Quick Start PBcR (老版的canu) CA Canu specializes in(专门从事) assembling PacBio or O ...

  3. <转>java 快速查找

    [Ct rl+T] 搜索当前接口的实现类 1. [ALT +/]    此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ ...

  4. 基于OGG的Oracle与Hadoop集群准实时同步介绍

    版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/220 来源:腾云阁 https://www.qclou ...

  5. java 内部类2(成员内部类)

    成员内部类: 特点:在其所在的外部类,的成员函数中,的类. 难点:看注释(涉及到jvm) /*test()执行完毕时,x2从内存中消失,inner的声明周,比x2长,inner还在访问,给人的感觉好像 ...

  6. The Economist

      The turning point in the process of growing up is when you discover the core of strength within yo ...

  7. python 练习 22

    Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false. Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句…… el ...

  8. grouping sets,cube,rollup,grouping__id,group by

    例1: hive -e" select type ,status ,count(1) from usr_info where pt='2015-09-14' group by type,st ...

  9. postgresql 触发器

    一.创建事件触发器 1.ddl_command_start - 一个DDL开始执行前被触发: 2.ddl_command_end - 一个DLL 执行完成后被触发: 3.sql_drop -- 删除一 ...

  10. Symmetric Tree [LeetCode]

    Problem description: http://oj.leetcode.com/problems/symmetric-tree/ Basic idea: Both recursive and ...