XZ_iOS之使用textView实现占位文字
2016-07-04 14:14
344 查看
众所周知,textField有占位文字,但是很多时候textfield并不能实现我们的需求,这时候就要用到textView了,但是textView又没有占位文字,所以,我今天就是使用textView实现有占位文字
XZTextView.h中
/** 占位文字 */
@property (nonatomic,
strong) NSString *placeholder;
XZTextView.m中
#import "UIView+Extension.h"
@interface XZTextView ()
/**
* 占位文字Label
*/
@property (weak,
nonatomic) UILabel *phLabel;
@end
@implementation XZTextView
- (UILabel *)phLabel
{
if (!_phLabel) {
UILabel *phLabel = [[UILabel
alloc] init];
//
文字自动换行
phLabel.numberOfLines =
0;
phLabel.x =
4;
phLabel.y =
7;
[self addSubview:phLabel];
self.phLabel = phLabel;
}
return
_phLabel;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super
initWithFrame:frame]) {
self.phLabel.textColor = [UIColor
lightGrayColor];
// 添加监听器,监听自己的文字改变通知
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(textDidChange)
name:UITextViewTextDidChangeNotification
object:nil];
}
return
self;
}
// 时刻监听文字键盘文字的变化,文字一旦改变便调用setNeedsDisplay方法
- (void)textDidChange
{
//
有文字就隐藏
self.phLabel.hidden =
self.hasText;
}
- (void)layoutSubviews
{
[super
layoutSubviews];
// 确定label的宽度,高度由文字数量自动计算
CGSize size =
CGSizeMake(self.width -
2 * self.phLabel.x,
MAXFLOAT);
// 根据文字的字体属性、文字的数量动态计算label的尺寸
self.phLabel.size
= [self.placeholder
boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName :
self.font}
context:nil].size;
}
// 占位文字
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
self.phLabel.text = placeholder;
//
更新文字尺寸
[self
setNeedsLayout];
}
// 字体
- (void)setFont:(UIFont *)font
{
[super setFont:font];
self.phLabel.font = font;
[self
setNeedsLayout];
}
- (void)dealloc
{
[[NSNotificationCenter
defaultCenter] removeObserver:self];
}
@end
XZTextView.h中
/** 占位文字 */
@property (nonatomic,
strong) NSString *placeholder;
XZTextView.m中
#import "UIView+Extension.h"
@interface XZTextView ()
/**
* 占位文字Label
*/
@property (weak,
nonatomic) UILabel *phLabel;
@end
@implementation XZTextView
- (UILabel *)phLabel
{
if (!_phLabel) {
UILabel *phLabel = [[UILabel
alloc] init];
//
文字自动换行
phLabel.numberOfLines =
0;
phLabel.x =
4;
phLabel.y =
7;
[self addSubview:phLabel];
self.phLabel = phLabel;
}
return
_phLabel;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super
initWithFrame:frame]) {
self.phLabel.textColor = [UIColor
lightGrayColor];
// 添加监听器,监听自己的文字改变通知
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(textDidChange)
name:UITextViewTextDidChangeNotification
object:nil];
}
return
self;
}
// 时刻监听文字键盘文字的变化,文字一旦改变便调用setNeedsDisplay方法
- (void)textDidChange
{
//
有文字就隐藏
self.phLabel.hidden =
self.hasText;
}
- (void)layoutSubviews
{
[super
layoutSubviews];
// 确定label的宽度,高度由文字数量自动计算
CGSize size =
CGSizeMake(self.width -
2 * self.phLabel.x,
MAXFLOAT);
// 根据文字的字体属性、文字的数量动态计算label的尺寸
self.phLabel.size
= [self.placeholder
boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName :
self.font}
context:nil].size;
}
// 占位文字
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
self.phLabel.text = placeholder;
//
更新文字尺寸
[self
setNeedsLayout];
}
// 字体
- (void)setFont:(UIFont *)font
{
[super setFont:font];
self.phLabel.font = font;
[self
setNeedsLayout];
}
- (void)dealloc
{
[[NSNotificationCenter
defaultCenter] removeObserver:self];
}
@end
相关文章推荐
- iOS10 权限崩溃问题
- ios判断是否有权限访问相机,相册,定位
- BGRA转灰度数据
- iOS音频篇:AVPlayer的缓存实现
- iOS 狂霸酷炫拽之Button动效
- XZ_iOS之使用GCD实现发送验证码
- iOS国际化
- iOS开发推荐DMG资源
- iOS动效学习-推荐资料(最全)
- iOS旋钮动画-CircleKnob
- [HAXE] haxe接入IOS版FacebookSdK以及iOS开发遇到的问题总结(二)
- iOS 获取当月最后一天或获取当月共有多少天
- iOS性能优化技巧
- iOS 创建单例的三种方法
- iOS视频开发经验
- 让iOS开发变得更有效率-分类、工具类
- iOS 沙盒目录结构和文件操作(NSFileManager)(一)
- iOS Storyboards Container View Controller整理笔记
- iOS动效
- IOS 开发环境,证书和授权文件