本文转载至 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. iOS导航栏背景,标题和返回按钮文字颜色

    在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...

  2. 一款基于jquery的手风琴图片展示效果

    今天要给大家分享一款基于jquery的手风琴图片展示效果.这款图片的展示效果鼠标经过前是灰色的,当鼠标经过时图片变大且变为彩色.效果图如下: 在线预览   源码下载 实现的代码. html代码: &l ...

  3. 一款由css3和jquery实现的卡面折叠式菜单

    之前已经为大家介绍了好多导航菜单.今天为大家再带来一款由css3和jquery实现的卡片折叠式菜单.当菜单关闭的时候,有三维堆叠的效果.我们一起看下效果图: 在线预览   源码下载 html代码: & ...

  4. Springmvc mvc:exclude-mapping不拦截 无效

    最近在使用ssm框架搭建一个后台,需要判断每个请求都判断是否登录,未登录跳转到登录页面, 使用发现 mvc:exclude-mapping 不拦截,发现,怎么都无效,依然是进行拦截.查了很多资料,总算 ...

  5. 关于使用samba用户的权限设置

    首先要保证你的samba安装并配置好,关于安装和配置samba请参考此文章http://blog.csdn.net/linglongwunv/archive/2010/01/19/5212875.as ...

  6. u-boot 2016.05 添加u-boot cmd

    记录一下如何在u-boot 添加一个自己想要的命令. 首先来看一下宏,include/command.h 218 #define U_BOOT_CMD(_name, _maxargs, _rep, _ ...

  7. 关于一致性Hash算法

    在大型web应用中,缓存可算是当今的一个标准开发配置了.在大规模的缓存应用中,应运而生了分布式缓存系统.分布式缓存系统的基本原理,大家也有所耳闻.key-value如何均匀的分散到集群中?说到此,最常 ...

  8. synchronize模块

    synchronize模块 使用rsync同步文件,其参数如下: archive: 归档,相当于同时开启recursive(递归).links.perms.times.owner.group.-D选项 ...

  9. 公司内网成功实现WSUS在不连外网的条件下更新补丁包!

    微软的WSUS的命令行很有帮助! 为了便于管理,WSUS服务器中提供了一个命令行工具WSUSUtil.exe,你可以使用它完成一些在WSUS管理控制台中不能进行的任务,例如导入导出数据等等.WSUSU ...

  10. js彈出層或者js彈出引用url Frame 層

    function Popup() { var _this = this; this.CssName = "layermask";//樣式 //遮蓋層 this.hiddLayer ...