本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html

在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能

介绍两种方法来实现:

第一种:

初始化UITextView

  1. //首先定义UITextView
  2. UITextView *textView = [[UITextView alloc] init];
  3. textView.font = [UIFont systemFontOfSize:14];
  4. textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);
  5. textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  6. textView.backgroundColor = [UIColor whiteColor];
  7. [cell.contentView addSubview:textView];
  8. textView.hidden = NO;
  9. textView.delegate = self;
  10. //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
  11. uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);
  12. uilabel.text = @"请填写审批意见...";
  13. uilabel.enabled = NO;//lable必须设置为不可用
  14. uilabel.backgroundColor = [UIColor clearColor];
  15. [cell.contentView addSubview:uilabel];

实现UITextView的代理

  1. -(void)textViewDidChange:(UITextView *)textView
  2. {
  3. self.examineText =  textView.text;
  4. if (textView.text.length == 0) {
  5. uilabel.text = @"请填写审批意见...";
  6. }else{
  7. uilabel.text = @"";
  8. }
  9. }

第二种:

UITextView 实现 placeholder 及隐藏键盘

#import <Foundation/Foundation.h>

@interface UIPlaceHolderTextView : UITextView {

NSString *placeholder;

UIColor *placeholderColor;

@private

UILabel *placeHolderLabel;

}

@property(nonatomic, retain) UILabel *placeHolderLabel;

@property(nonatomic, retain) NSString *placeholder;

@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end

#import "UIPlaceHolderTextView.h"

@implementation UIPlaceHolderTextView

@synthesize placeHolderLabel;

@synthesize placeholder;

@synthesize placeholderColor;

- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

[placeHolderLabel release]; 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] == 0)

{

return;

}

if([[self text] length] == 0)

{

[[self viewWithTag:999] setAlpha:1];

}

else

{

[[self viewWithTag:999] setAlpha:0];

}

}

- (void)setText:(NSString *)text {

[super setText:text];

[self textChanged:nil];

}

- (void)drawRect:(CGRect)rect

{

if( [[self placeholder] length] > 0 )

{

if ( placeHolderLabel == nil )

{

placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];

placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;

placeHolderLabel.numberOfLines = 0;

placeHolderLabel.font = self.font;

placeHolderLabel.backgroundColor = [UIColor clearColor];

placeHolderLabel.textColor = self.placeholderColor;

placeHolderLabel.alpha = 0;

placeHolderLabel.tag = 999;

[self addSubview:placeHolderLabel];

}

placeHolderLabel.text = self.placeholder;

[placeHolderLabel sizeToFit];

[self sendSubviewToBack:placeHolderLabel];

}

if( [[self text] length] == 0 && [[self placeholder] length] > 0 )

{

[[self viewWithTag:999] setAlpha:1];

}

[super drawRect:rect];

}

@end

//隐藏键盘,实现UITextViewDelegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

{

if ([text isEqualToString:@"\n"]) {

[m_textView resignFirstResponder];

return NO;

}

return YES;

}

UITextView 实现placeholder的方法的更多相关文章

  1. UITextView实现placeHolder方法汇总

    UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用.可是UITextView就没那么幸运了,apple没有给UITextView提供 ...

  2. 教大家怎样给UITextView加入placeholder扩展

    怎样扩展UITextView以追加placeholder功能呢? 我们的需求是:追加placeholder功能 方案讨论: 通过继承UITextView的方式 通过扩展UITextView的方式 分析 ...

  3. iOS - UITextView实现placeHolder占位文字

      iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...

  4. UITextView实现PlaceHolder的方式

    实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder:下面来介绍两种方法:个人比较喜欢第一种,看起来更加合理. 方法1: ...

  5. placeholder兼容方法(兼容IE8以上浏览器)

    //placeholder兼容方法(兼容IE8以上浏览器) var JPlaceHolder = { //检测 _check: function () { return 'placeholder' i ...

  6. 实现UITextView的placeholder

    我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...

  7. UITextView设置placeholder

    下面是我的代码,可以直接拿来用 #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property(nonat ...

  8. iOS开发-UITextView实现PlaceHolder的方式

    之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...

  9. 【转】Spring项目启动报"Could not resolve placeholder"解决方法

    问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceho ...

随机推荐

  1. position定位属性

    值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", " ...

  2. 基于jQuery点击圆形边框弹出图片信息

    分享一款基于jQuery点击圆形边框弹出图片信息.这是一款鼠标经过图片转换成圆形边框,点击可弹出文字信息.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id=&q ...

  3. cmder小技巧

    1.修改config下的aliases文件,可以添加别名 $* 表示所有参数,$i 表示第几个参数 比如cd cd=cd /d $* 这样window下的cd就可以直接切换盘符+路径了. 有用的别名 ...

  4. mysql 5.7.12----bin/mysqld --initialize --user=mysql出错

    我最近在安装mysql 5.7.12,本来之前安装mysql 5.7.11时用命令 bin/mysqld --initialize --user=mysql 可以很好的初始化,但是用在5.7.12版本 ...

  5. ElasticSearch自定义分析器-集成结巴分词插件

    关于结巴分词 ElasticSearch 插件: https://github.com/huaban/elasticsearch-analysis-jieba 该插件由huaban开发.支持Elast ...

  6. WPF路由事件三:自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...

  7. Rattle:数据挖掘的界面化操作

    R语言是一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具.这里的统计计算可以是数据分析.建模或是数据挖掘等,通过无数大牛提供的软件包,可以帮我们轻松实现算法的实施. 一些读者 ...

  8. 关于Struts2有时候出现的莫名其妙的错误

    有的时候突然出现红色叉叉,但是又不知道哪里错了,解决方法: 1.刷新项目文件夹 2.重启MyEclipse

  9. netsnmp编译动态库

    .编译动态库 将写完的snmp代理程序编译生成动态库 gcc -c -fpic telnetConfig.c -o telnetConfig.o -I/usr/local/net-snmp/inclu ...

  10. C++ 类的隐式转换

    所谓类的隐式转换,就是将实参类型转成形参类型--如果不一致的话. 这个转换与基本类型转换不太一样,具体则是在形参类型的构造函数中使用实参类型的数据,从而构造出一个临时对象. 下面的代码,类Person ...