textView实现placehodle
2016-07-13 17:47
387 查看
.H文件里面需要实现:
#import <UIKit/UIKit.h>
@interface UIPHTextView :
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
.M需要实现:
#import "UIPHTextView.h"
@implementation UIPHTextView
- (instancetype)init{
self = [super init];
if (self) {
[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification
object:nil];
}
return
self;
}
- (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 = [UIFont
systemFontOfSize:14];
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
ViewController.m里面需要实现:
#import "ViewController.h"
#import "UIPHTextView.h"
@interface ViewController ()<UITextViewDelegate>
{
UIView *_bgView;
UIPHTextView *_textView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
_createView];
}
- (void)_createView{
if (_bgView ==
nil) {
_bgView = [[UIView
alloc]initWithFrame:CGRectMake(0,
10, self.view.frame.size.width,
200)];
_bgView.backgroundColor = [UIColor
greenColor];
[self.view
addSubview:_bgView];
}
_textView = [[UIPHTextView
alloc] initWithFrame:CGRectMake(5,
5, self.view.frame.size.width
- 10, 200 -
85)];
_textView.placeholder =
@"说点什么...";
_textView.font = [UIFont
systemFontOfSize:15];
_textView.delegate =
self;
_textView.showsVerticalScrollIndicator =
NO;
_textView.placeholderColor = [UIColor
grayColor];
[_bgView
addSubview:_textView];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface UIPHTextView :
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
.M需要实现:
#import "UIPHTextView.h"
@implementation UIPHTextView
- (instancetype)init{
self = [super init];
if (self) {
[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification
object:nil];
}
return
self;
}
- (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 = [UIFont
systemFontOfSize:14];
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
ViewController.m里面需要实现:
#import "ViewController.h"
#import "UIPHTextView.h"
@interface ViewController ()<UITextViewDelegate>
{
UIView *_bgView;
UIPHTextView *_textView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
_createView];
}
- (void)_createView{
if (_bgView ==
nil) {
_bgView = [[UIView
alloc]initWithFrame:CGRectMake(0,
10, self.view.frame.size.width,
200)];
_bgView.backgroundColor = [UIColor
greenColor];
[self.view
addSubview:_bgView];
}
_textView = [[UIPHTextView
alloc] initWithFrame:CGRectMake(5,
5, self.view.frame.size.width
- 10, 200 -
85)];
_textView.placeholder =
@"说点什么...";
_textView.font = [UIFont
systemFontOfSize:15];
_textView.delegate =
self;
_textView.showsVerticalScrollIndicator =
NO;
_textView.placeholderColor = [UIColor
grayColor];
[_bgView
addSubview:_textView];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- 通过注册表修改VC6.0的字体
- 不同数据库中查询前几条记录的用法(SQL Server/MYSQL /Oracle/Postgresql)
- HTML网页开发学习笔记(燕十八)-41 字符实体
- php需要做的安全防护
- jQuery学习笔记之事件
- spark新能优化之多次使用RDD的持久化或checkPoint
- 以SpringMVC框架为中心疯狂扩展-03、添加springframework的依赖
- AARRR:互联网创业者一定要掌握的指标
- OpenSSLRSA.h
- Git 常用操作
- oracle客户端精简绿色版-环境变量配置
- log4j.properties配置详解
- HTML5+、mui开发移动app
- php常用加密技术(包括密码加密及URL加密)
- NYOJ-开灯问题
- 实习期间移动端做时总结
- BZOJ3548 : [ONTAK2010]Party
- JAVA-实用类
- css3变形中几个重要点
- 从Dinnr失败看产品市场可行性认知有哪些不足