UITextView 添加placeholder
来源:互联网 发布:现在淘宝卖什么挣钱 编辑:程序博客网 时间:2024/05/16 09:31
1、首先定义一个类UIPlaceHolderTextView继承UITextView.
头文件如下:
#import <UIKit/UIKit.h>@interface UIPlaceHolderTextView : UITextView@property (nonatomic,retain) NSString *placeHolder;@property(nonatomic,retain) UIColor *placeHolderColor;-(void)textChanged:(NSNotification *)notification;@end
实现内容代码如下:
#import "UIPlaceHolderTextView.h"@interface UIPlaceHolderTextView()@property(nonatomic,retain) UILabel *placeHolderLabel;@end@implementation UIPlaceHolderTextView-(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; [_placeHolder release]; _placeHolder=nil; [_placeHolderColor release]; _placeHolderColor=nil; [_placeHolder release]; _placeHolder=nil; [super dealloc];}-(void)awakeFromNib{ [super awakeFromNib]; if(!self.placeHolder){ [self setPlaceHolder:@""]; } if(!self.placeHolderColor){ [self setPlaceHolderColor:[UIColor lightGrayColor]]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidBeginEditingNotification object:nil];}-(void)textChanged:(NSNotification *)notification{ if([[self placeHolder] length]==0){ return; } if([[self text] length]==0){ [[self viewWithTag:1000] setAlpha:1]; }else{ [[self viewWithTag:1000] setAlpha:0]; }}-(void)setText:(NSString *)text{ [super setText:text]; [self textChanged:nil];} // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. -(void)drawRect:(CGRect)rect { // Drawing code if([[self placeHolder] length]>0){ if(_placeHolderLabel==nil){ _placeHolderLabel=[[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width-16,0)]; _placeHolderLabel.lineBreakMode=NSLineBreakByCharWrapping; _placeHolderLabel.numberOfLines=0; _placeHolderLabel.font=self.font; _placeHolderLabel.backgroundColor=[UIColor clearColor]; _placeHolderLabel.textColor=self.placeHolderColor; _placeHolderLabel.alpha=0; _placeHolderLabel.tag=1000; [self addSubview:_placeHolderLabel]; } _placeHolderLabel.text=self.placeHolder; [_placeHolderLabel sizeToFit]; [self sendSubviewToBack:_placeHolderLabel]; } if([[self text] length]==0&&[[self placeHolder] length]>0){ [[self viewWithTag:1000] setAlpha:1]; } [super drawRect:rect]; } - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code [self setPlaceHolderColor:[UIColor lightGrayColor]]; [self setPlaceHolder:@""]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidBeginEditingNotification object:nil]; } return self;}@end
2.UIPlaceHolderTextView的调用
调用方法如下,首先在xib上面拖放一个UITextView,让它继承UIPlaceHolderTextView或者直接创建一个UIPlaceHolderTextView添加到xib上面,这里我们拖放一个UITextView让它继承UIPlaceHolderTextView,代码如下:#import <UIKit/UIKit.h>#import "UIPlaceHolderTextView.h"@interface LTViewController : UIViewController@property(nonatomic,retain) IBOutlet UIPlaceHolderTextView *textview;//定义一个UIPlaceHolderTextView@end
实现代码如下:
#import "LTViewController.h"#import <QuartzCore/QuartzCore.h>@interface LTViewController ()<UITextViewDelegate>@end@implementation LTViewController@synthesize textview;-(void)dealloc{ [textview release]; [super dealloc];}- (void)viewDidLoad{ [super viewDidLoad];
//设置一下UITextView的样式,需用用的<QuartzCore/QuartzCore.h> [textview.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; [textview.layer setBorderWidth:2.0]; textview.layer.cornerRadius=10; textview.clipsToBounds=YES; textview.delegate=self; textview.placeHolder=@"请输入详细信息";//设置textview的placeHolder的显示内容// Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
- UITextView 添加placeholder
- UITextView添加Placeholder(swift)
- UITextView添加placeholder属性
- 给UITextView添加PlaceHolder
- UITextView添加placeholder属性
- UITextView添加placeHolder
- UITextView添加placeholder属性
- 【iOS】UITextView中添加 placeholder
- iOS给UITextView添加Placeholder
- ios UITextView 添加水印提示(Placeholder)
- 给UITextView添加类似placeholder功能
- UItextView Placeholder
- UITextView placeholder
- 关于如何在UITextView等添加输入提示(placeholder)
- 教大家如何给UITextView添加placeholder扩展
- DeveKing7-给UITextView添加Placeholder最简单易懂的方法
- UITextView的字数限制 及 添加自定义PlaceHolder
- UITextView 实现placeholder
- Hello.lua注释
- php:根据中文裁减字符串函数方法
- 基于51单片机-温度监控系统
- 仪表
- Oracle 培训笔记
- UITextView 添加placeholder
- 1-0组成的串中,1和0个数相等的最长子串
- “黑马程序员”宣传语有奖大征集《参赛作品》
- Telnet HTTP 测试步骤 解决返回400 bad Request
- C++实现全排列
- php:检测用户当前浏览器是否为IE浏览器
- 教你如何使用libpng显示png图片
- vm会不定时地宕机
- 一个公司的管理之三:在公布任何的任命、制度之前一定要三思