本文转载至 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. json关键总结

    先引用 Newtonsoft.Json.Net20.dll //序列化对象的方法也非常的简单: string json = JsonConvert.SerializeObject(product); ...

  2. java获取当月的第一天和最后一天,获取本周的第一天和最后一天

    /** * 获取指定日期所在周的第一天和最后一天,用下划线连接 * @param dataStr * @return * @throws ParseException */ public static ...

  3. java中的访问控制符

    首先来一张图: 对于一个类而言访问控制符只有一个public和默认无修饰符.其他的几个访问修饰符对于变量和方法都可以使用. 下面介绍具体的使用. 1. 公有访问控制符(public) Java的类是通 ...

  4. Eclipse里安装插件

    1.在eclipse中选择 help->install new software. 2.在work with 框中输入:Indigo - http://download.eclipse.org/ ...

  5. 一、think in java 第一章

    一.面向对象程序设计方式: 1.万物皆为对象. 将对象视为奇特的变量,它可以存储数据,也可以要求它在自身上执行操作. 2.程序是对象的集合,它们通过发送消息来告知彼此所要做的. 要请求一个对象,就必须 ...

  6. [转]ViewPager学习笔记(一)——懒加载

    在项目中ViewPager和Fragment接口框架已经是处处可见,但是在使用中,我们肯定不希望用户在当前页面时就在前后页面的数据,加入数据量很大,而用户又不愿意左右滑动浏览,那么这时候ViewPag ...

  7. JavaScript中的Boolean 方法与Number方法

    <html> <head> <script type="text/javascript"> //创建 var str = "aaafg ...

  8. jsp页面积累

    out.print(页面html);response.setContexttype("");以页面的形式展现java

  9. 关于Cocos2d-x的粒子系统

    1.cocos2d-x有一些自带的粒子效果,以后可以用到.当然,也可以自己定义一些粒子,不过要定义的话,虽然可以用cpp文件自己写,但是没有可视化的调节,还要设定各种奇怪的参数,是非常困难的.可以用一 ...

  10. try catch 异常处理

    1.捕获指定异常 2.捕获所有异常(catch(...))