本文转载至 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. iOSGCD的使用以及死锁的问题

    死锁一直都是在使用多线程时,需要注意的一个问题.以前对同步.异步,串行.并行只有一个模糊的概念,想想也是时候整理一下了.再看看之前的博客,已经很久没有干货了[说得好像之前有干货一样],所以,这篇博客, ...

  2. 模板方法模式(Head first 设计模式——7)

    一.模板方法模式定义 模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 从定义中,应该可以看出一部分,为 ...

  3. flume sourcetype avro http

    flume  当sourcetype=http的时候可以用 curl -X POST -d '[{"headers" :{"APPSTORE" : " ...

  4. 【WPF】XAML实现按钮背景图片的点击切换

    原因:要做一组搜索结果的排序按钮(类似一组RadioButton),效果像下图这样.想法是使用原生的按钮控件,将文字左对齐,整个按钮背景是一张图片,通过样式Trigger控制字体变色.背景图切换. 需 ...

  5. Android——RadioGroup和CheckBox

    xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  6. 9260与SAM-BA连接(转)

    对于AT91SAM9260的bootloader的烧写,常常会遇到这样的问题:对于干净的NAND FLASH(即没有烧写过任何东西),AT91SAM9260与sam-ba很容易连接成功,但当烧写过bo ...

  7. EasyUI效果--DataGrid的编辑效果

    DataGrid的编辑效果是我目前使用的easyUI的第三个效果,相对于前两个,这个算是比较复杂点了.    运行起来的效果,大概就是这样,任意点击某行,然后该行变为可以编辑的,失去焦点之后,该行恢复 ...

  8. [oracle] 系统权限管理

    1 利用超级用户连接到数据库登录 sqlplus / as sysdba or slqplus SYS/SYSPWD as sysdba 这两个命令的效果是一样的,都是以sysdba的身份使得SYS管 ...

  9. 质量管理是SQA(软件质量保证)人员的职责

    质量管理是SQA(软件质量保证)人员的职责

  10. JAVA 多线程机制(二)

    主要内容 1.理解线程的并发性 2.线程的同步 3.线程的常用方法   上一章中由于线程的并发性导致了多线程的执行总是会出现一些问题..线程的并发性是程序员不可控制 的,也是不可避免的,线程的并发性往 ...