UITextView添加一个placeholder功能
2015-07-02 23:58
387 查看
控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行输入。2.UITextField有placeholder属性,而UITextView没有。至于两者的代理方法,原理基本差不多,只是方法名略有差异。
实现该功能有两种方式 一种是 ①使用通知 显示隐藏遮盖物
②使用代理 给文本框重新赋值
1.在创建textView的时候,赋值其文本属性
即textView.text = @"想说的话";
2.在开始编辑和结束编辑的代理方法中进行判断
推荐使用第一种
实现该功能有两种方式 一种是 ①使用通知 显示隐藏遮盖物
②使用代理 给文本框重新赋值
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(150, 150, 100, 30)]; text1.backgroundColor = [UIColor grayColor]; text1.placeholder = @"请输入账号"; text1.clearButtonMode = UITextFieldViewModeWhileEditing; [self.view addSubview:text1]; // 做法① 使用通知 用一个UIlabel遮盖再UItextView上面 根据输入文字是否为0来隐藏显示遮盖物 文字提示 DJTextView *text = [[DJTextView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; text.backgroundColor = [UIColor grayColor]; text.placeholder = @"请输入账号"; text.placeholderColor = [UIColor whiteColor]; [self.view addSubview:text]; // 做法② 使用UITextViewDelegate代理 开始编辑和结束编辑事件重新赋值文本属性 _placeHolderText = [[DJplaceHolderTextView alloc]initWithFrame:CGRectMake(250, 250, 100, 100)]; _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 < 1) { 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] == 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 ( self.placeholderLabel == nil ) { self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)]; self.placeholderLabel.lineBreakMode = UILineBreakModeWordWrap;//过时的 self.placeholderLabel.numberOfLines = 0; self.placeholderLabel.font = self.font; self.placeholderLabel.backgroundColor = [UIColor clearColor]; self.placeholderLabel.textColor = self.placeholderColor; self.placeholderLabel.alpha = 0; self.placeholderLabel.tag = 999; [self addSubview:self.placeholderLabel]; } self.placeholderLabel.text = self.placeholder; [self.placeholderLabel sizeToFit]; [self sendSubviewToBack:self.placeholderLabel]; } if( [[self text] length] == 0 && [[self placeholder] length] > 0 ) { [[self viewWithTag:999] setAlpha:1]; } [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
推荐使用第一种
相关文章推荐
- ios 数据类型转换 UIImage转换为NSData NSData转换为NSString
- 5 Drease-and-Conquer Fake-Coin Problem(减治法求假币问题)
- UIScrollView+UIPageControl实现图片分页
- LeetCode_Disjoint-Set_Longest Consecutive Sequence
- UI基础:视图控制器.屏幕旋转.MVC
- UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏
- IOS键盘的相关设置(UITextfield)
- IOS--UI--LessonUILabel
- IOS--UI--LessonButton UIImage
- IOS菜鸟的所感所思(十五)—— UIScrollView和UIPageControl的组合
- 快速的判断两个字符串型数组是否有交集
- iOS学习笔记 (9) UITabBarController分栏控制器
- Map存储 (key, value)
- iOS学习笔记(6)UINavigationController
- Android5.0L因SystemUI ANR导致的黑屏问题分析
- iOS学习笔记(4)UIView和UIImageView
- UIButton/按钮
- uCGUI 按键切换对话框内子控件焦点
- 3000套IOS android源码分享 7.2G UI素材
- 加班~~~第二天之自定义UIPageControl控件