UITextView 实现placeholder的方法
本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html
在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能
介绍两种方法来实现:
第一种:
初始化UITextView
- //首先定义UITextView
- UITextView *textView = [[UITextView alloc] init];
- textView.font = [UIFont systemFontOfSize:14];
- textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);
- textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- textView.backgroundColor = [UIColor whiteColor];
- [cell.contentView addSubview:textView];
- textView.hidden = NO;
- textView.delegate = self;
- //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
- uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);
- uilabel.text = @"请填写审批意见...";
- uilabel.enabled = NO;//lable必须设置为不可用
- uilabel.backgroundColor = [UIColor clearColor];
- [cell.contentView addSubview:uilabel];
实现UITextView的代理
- -(void)textViewDidChange:(UITextView *)textView
- {
- self.examineText = textView.text;
- if (textView.text.length == 0) {
- uilabel.text = @"请填写审批意见...";
- }else{
- uilabel.text = @"";
- }
- }
第二种:
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的方法的更多相关文章
- UITextView实现placeHolder方法汇总
UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用.可是UITextView就没那么幸运了,apple没有给UITextView提供 ...
- 教大家怎样给UITextView加入placeholder扩展
怎样扩展UITextView以追加placeholder功能呢? 我们的需求是:追加placeholder功能 方案讨论: 通过继承UITextView的方式 通过扩展UITextView的方式 分析 ...
- iOS - UITextView实现placeHolder占位文字
iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...
- UITextView实现PlaceHolder的方式
实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder:下面来介绍两种方法:个人比较喜欢第一种,看起来更加合理. 方法1: ...
- placeholder兼容方法(兼容IE8以上浏览器)
//placeholder兼容方法(兼容IE8以上浏览器) var JPlaceHolder = { //检测 _check: function () { return 'placeholder' i ...
- 实现UITextView的placeholder
我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...
- UITextView设置placeholder
下面是我的代码,可以直接拿来用 #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property(nonat ...
- iOS开发-UITextView实现PlaceHolder的方式
之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...
- 【转】Spring项目启动报"Could not resolve placeholder"解决方法
问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceho ...
随机推荐
- Excel VBA 操作 Word(入门篇)
原文地址 本文的对象是:有一定Excel VBA基础,对Word VBA还没有什么认识,想在Excel中通过VBA操作Word还有困难的人. 一.新建Word引用 需要首先创建一个对 Word A ...
- Linux网络故障排查
1.先排查网络配置信息 IP地址->子网掩码->网关->DNS 2.查看到达的网关是否连通 ping IP地址. 3.查看DNS解析是否正常.
- android 相机拍照后选择照片编辑,相片编辑界面直线形状会显示锯齿状
因为 decode 出来的图片太小,小于屏幕.所以,显示的时候 会把图片略微放大,导致直线形状会显示锯齿状. 能够改动getScreenImageSize 方法中的size 的大小,比方能够把13 ...
- Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”
Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization” 虚拟机Vmware上克隆了一个Red ...
- windows用命令行查看硬件信息
如何在windows系统自带命令查看硬件信息,怎样dos命令查看硬盘和内存/CPU信息?最直接的是:开始→运行→CMD打开命令提示符,在该窗口下输入systeminfo执行,即可看到几乎所有想知道的系 ...
- ajax传值 乱码问题
ajax.overrideMimeType("text/xml;charset=GBK");
- thikphp 简单的接口骨架
//get id 获取内容,调用xml方法 public function get(){ $id = $_GET['id'];//接收id $User = M('user'); //$val-> ...
- 【转】【c++】指针参数是如何传递内存的
参数策略 如果函数的参数是一个指针,不要指望用该指针去动态申请内存.如下: void GetMemory(char *p, int num) { p = (char *)malloc(sizeof(c ...
- (转)一种开源的跨平台视频开发框架:VideoLAN - VLC media player
VLC原先是几个法国的大学生做的项目,后来他们把VLC作为了一个开源的项目,吸引了来自世界各国的很多优秀程序员来共同编写和维护VLC,才逐渐变成了现在这个样子.至于为什么叫VideoLan Clien ...
- DTD -- XML验证
DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. DTD简介 内部的 DOCTYPE 声明 假如 DTD 被包含在您的 ...