控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行输入。2.UITextField有placeholder属性,而UITextView没有。至于两者的代理方法,原理基本差不多,只是方法名略有差异。
实现该功能有两种方式 一种是 ①使用通知 显示隐藏遮盖物
②使用代理 给文本框重新赋值
1.在创建textView的时候,赋值其文本属性
即textView.text = @"想说的话";
2.在开始编辑和结束编辑的代理方法中进行判断
//
// ViewController.m
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "ViewController.h"
#import "DJTextView.h"
#import "DJplaceHolderTextView.h"
@interface ViewController () <UITextViewDelegate>
@property (nonatomic,strong)DJplaceHolderTextView *placeHolderText;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
text1.backgroundColor = [UIColor grayColor];
text1.placeholder = @"请输入账号";
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:text1]; // 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
DJTextView *text = [[DJTextView alloc]initWithFrame:CGRectMake(, , , )];
text.backgroundColor = [UIColor grayColor];
text.placeholder = @"请输入账号";
text.placeholderColor = [UIColor whiteColor];
[self.view addSubview:text]; // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
_placeHolderText = [[DJplaceHolderTextView alloc]initWithFrame:CGRectMake(, , , )];
_placeHolderText.backgroundColor = [UIColor grayColor];
_placeHolderText.placeholder = @"请输入账号";
_placeHolderText.text = _placeHolderText.placeholder;
_placeHolderText.delegate = self;
[self.view addSubview:_placeHolderText];
}
/**
* 开始编辑事件
*/
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder);
if ([textView.text isEqualToString:self.placeHolderText.placeholder]) {
textView.text = @"";
}
}
/**
* 结束编辑事件
*/
- (void)textViewDidEndEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder);
if (textView.text.length < ) {
textView.text = self.placeHolderText.placeholder;
}
} @end
// 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
//
// DJplaceHolderTextView.h
// 绘制TextView
//
// Created by zjj on 15/7/2.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import <UIKit/UIKit.h> @interface DJplaceHolderTextView : UITextView
/**
* placehold 用户输入前文本提示
*/
@property (nonatomic,copy) NSString *placeholder;
@end
// 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
//
// DJTextView.h
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import <UIKit/UIKit.h>
/**
* UITextView 实现 placeholder 及隐藏键盘
*/
@interface DJTextView : UITextView
/**
* placehold 用户输入前文本提示
*/
@property (nonatomic,copy) NSString *placeholder;
/**
* placeholder 文字颜色
*/
@property (nonatomic,strong)UIColor *placeholderColor;
/**
* placeholder 提示label
*/
@property (nonatomic,strong)UILabel *placeholderLabel;
/**
* 文本改变事件
*/
-(void)textChanged:(NSNotification*)notification;
@end
//
// DJTextView.m
// 绘制TextView
//
// Created by zjj on 15/7/1.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "DJTextView.h" @implementation DJTextView //-(void)setPlaceholder:(NSString *)placeholder
//{
// _placeholder = placeholder;
//
// [self setNeedsDisplay];
//}
//
//- (void)drawRect:(CGRect)rect
//{
//
// [self.placeholder drawInRect:rect withAttributes:nil];
//} - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; // 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] == ) { return; } if([[self text] length] == ) { [[self viewWithTag:] setAlpha:]; } else { [[self viewWithTag:] setAlpha:]; } } - (void)setText:(NSString *)text { [super setText:text]; [self textChanged:nil]; } - (void)drawRect:(CGRect)rect { if( [[self placeholder] length] > ) { if ( self.placeholderLabel == nil ) { self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(,,self.bounds.size.width - ,)];
self.placeholderLabel.lineBreakMode = UILineBreakModeWordWrap;//过时的
self.placeholderLabel.numberOfLines = ; self.placeholderLabel.font = self.font; self.placeholderLabel.backgroundColor = [UIColor clearColor]; self.placeholderLabel.textColor = self.placeholderColor; self.placeholderLabel.alpha = ; self.placeholderLabel.tag = ; [self addSubview:self.placeholderLabel]; } self.placeholderLabel.text = self.placeholder; [self.placeholderLabel sizeToFit]; [self sendSubviewToBack:self.placeholderLabel]; } if( [[self text] length] == && [[self placeholder] length] > ) { [[self viewWithTag:] setAlpha:]; } [super drawRect:rect]; }
////隐藏键盘,实现UITextViewDelegate
//
//-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
//
//{
//
// if ([text isEqualToString:@"\n"]) {
//
// [textView resignFirstResponder];
//
// return NO;
//
// }
//
// return YES;
//
//} @end
推荐使用第一种