UITextView添加一个placeholder功能
控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行输入。2.UITextField有placeholder属性,而UITextView没有。至于两者的代理方法,原理基本差不多,只是方法名略有差异。
实现该功能有两种方式 一种是 ①使用通知 显示隐藏遮盖物
②使用代理 给文本框重新赋值
1.在创建textView的时候,赋值其文本属性
即textView.text = @"想说的话";
2.在开始编辑和结束编辑的代理方法中进行判断
1 // 2 // ViewController.m 3 // 绘制TextView 4 // 5 // Created by zjj on 15/7/1. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "DJTextView.h" 11 #import "DJplaceHolderTextView.h" 12 @interface ViewController () <UITextViewDelegate> 13 @property (nonatomic,strong)DJplaceHolderTextView *placeHolderText; 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(150, 150, 100, 30)]; 21 text1.backgroundColor = [UIColor grayColor]; 22 text1.placeholder = @"请输入账号"; 23 text1.clearButtonMode = UITextFieldViewModeWhileEditing; 24 [self.view addSubview:text1]; 25 26 // 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示 27 DJTextView *text = [[DJTextView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; 28 text.backgroundColor = [UIColor grayColor]; 29 text.placeholder = @"请输入账号"; 30 text.placeholderColor = [UIColor whiteColor]; 31 [self.view addSubview:text]; 32 33 // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性 34 _placeHolderText = [[DJplaceHolderTextView alloc]initWithFrame:CGRectMake(250, 250, 100, 100)]; 35 _placeHolderText.backgroundColor = [UIColor grayColor]; 36 _placeHolderText.placeholder = @"请输入账号"; 37 _placeHolderText.text = _placeHolderText.placeholder; 38 _placeHolderText.delegate = self; 39 [self.view addSubview:_placeHolderText]; 40 } 41 /** 42 * 开始编辑事件 43 */ 44 - (void)textViewDidBeginEditing:(UITextView *)textView 45 { 46 // NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder); 47 if ([textView.text isEqualToString:self.placeHolderText.placeholder]) { 48 textView.text = @""; 49 } 50 } 51 /** 52 * 结束编辑事件 53 */ 54 - (void)textViewDidEndEditing:(UITextView *)textView 55 { 56 // NSLog(@"textViewDidBeginEditing%@ - %ld - %@",textView.text,textView.text.length,self.placeHolderText.placeholder); 57 if (textView.text.length < 1) { 58 textView.text = self.placeHolderText.placeholder; 59 } 60 } 61 62 @end
// 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性
1 // 2 // DJplaceHolderTextView.h 3 // 绘制TextView 4 // 5 // Created by zjj on 15/7/2. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface DJplaceHolderTextView : UITextView 12 /** 13 * placehold 用户输入前文本提示 14 */ 15 @property (nonatomic,copy) NSString *placeholder; 16 @end
// 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示
1 // 2 // DJTextView.h 3 // 绘制TextView 4 // 5 // Created by zjj on 15/7/1. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 /** 11 * UITextView 实现 placeholder 及隐藏键盘 12 */ 13 @interface DJTextView : UITextView 14 /** 15 * placehold 用户输入前文本提示 16 */ 17 @property (nonatomic,copy) NSString *placeholder; 18 /** 19 * placeholder 文字颜色 20 */ 21 @property (nonatomic,strong)UIColor *placeholderColor; 22 /** 23 * placeholder 提示label 24 */ 25 @property (nonatomic,strong)UILabel *placeholderLabel; 26 /** 27 * 文本改变事件 28 */ 29 -(void)textChanged:(NSNotification*)notification; 30 @end
1 // 2 // DJTextView.m 3 // 绘制TextView 4 // 5 // Created by zjj on 15/7/1. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "DJTextView.h" 10 11 @implementation DJTextView 12 13 14 //-(void)setPlaceholder:(NSString *)placeholder 15 //{ 16 // _placeholder = placeholder; 17 // 18 // [self setNeedsDisplay]; 19 //} 20 // 21 //- (void)drawRect:(CGRect)rect 22 //{ 23 // 24 // [self.placeholder drawInRect:rect withAttributes:nil]; 25 //} 26 27 - (void)dealloc 28 29 { 30 31 [[NSNotificationCenter defaultCenter] removeObserver:self]; 32 33 // placeHolderLabel = nil; 34 // 35 // [placeholderColor release]; placeholderColor = nil; 36 // 37 // [placeholder release]; placeholder = nil; 38 39 // [super dealloc]; 40 41 } 42 43 44 45 - (void)awakeFromNib 46 47 { 48 49 [super awakeFromNib]; 50 51 [self setPlaceholder:@""]; 52 53 [self setPlaceholderColor:[UIColor lightGrayColor]]; 54 55 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; 56 57 } 58 59 60 61 - (id)initWithFrame:(CGRect)frame 62 63 { 64 65 if( (self = [super initWithFrame:frame]) ) 66 67 { 68 69 [self setPlaceholder:@""]; 70 71 [self setPlaceholderColor:[UIColor lightGrayColor]]; 72 73 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; 74 75 } 76 77 return self; 78 79 } 80 81 82 83 - (void)textChanged:(NSNotification *)notification 84 85 { 86 87 if([[self placeholder] length] == 0) 88 89 { 90 91 return; 92 93 } 94 95 96 97 if([[self text] length] == 0) 98 99 { 100 101 [[self viewWithTag:999] setAlpha:1]; 102 103 } 104 105 else 106 107 { 108 109 [[self viewWithTag:999] setAlpha:0]; 110 111 } 112 113 } 114 115 116 117 - (void)setText:(NSString *)text { 118 119 [super setText:text]; 120 121 [self textChanged:nil]; 122 123 } 124 125 126 127 - (void)drawRect:(CGRect)rect 128 129 { 130 131 if( [[self placeholder] length] > 0 ) 132 133 { 134 135 if ( self.placeholderLabel == nil ) 136 137 { 138 139 self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)]; 140 self.placeholderLabel.lineBreakMode = UILineBreakModeWordWrap;//过时的 141 self.placeholderLabel.numberOfLines = 0; 142 143 self.placeholderLabel.font = self.font; 144 145 self.placeholderLabel.backgroundColor = [UIColor clearColor]; 146 147 self.placeholderLabel.textColor = self.placeholderColor; 148 149 self.placeholderLabel.alpha = 0; 150 151 self.placeholderLabel.tag = 999; 152 153 [self addSubview:self.placeholderLabel]; 154 155 } 156 157 158 159 self.placeholderLabel.text = self.placeholder; 160 161 [self.placeholderLabel sizeToFit]; 162 163 [self sendSubviewToBack:self.placeholderLabel]; 164 165 } 166 167 168 169 if( [[self text] length] == 0 && [[self placeholder] length] > 0 ) 170 171 { 172 173 [[self viewWithTag:999] setAlpha:1]; 174 175 } 176 177 178 179 [super drawRect:rect]; 180 181 } 182 ////隐藏键盘,实现UITextViewDelegate 183 // 184 //-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text 185 // 186 //{ 187 // 188 // if ([text isEqualToString:@"\n"]) { 189 // 190 // [textView resignFirstResponder]; 191 // 192 // return NO; 193 // 194 // } 195 // 196 // return YES; 197 // 198 //} 199 200 @end
推荐使用第一种