自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写setter,保证外界与内部的交互性

一.方案一:通过drawRect:方法将文字画到textView中,监听文字改变用的是通知中心(代理也可以监听文字改变,但是这种方式就成了自己作为自己的代理了,不推荐这种方法)发出的消息

UITextViewTextDidChangeNotification.自己监听通知.

  • 对外界提供两个属性

  • 内部实现
 #import "ChaosPlaceholdTextView.h"

 @implementation ChaosPlaceholdTextView

 - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil]; // 外界文字颜色不设置,默认为灰色
self.placeholdColor = [UIColor grayColor];
}
return self;
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} - (void)textChange
{
[self setNeedsDisplay];
} // 调用drawRect时,会将之前的图像擦除掉,重新绘制
- (void)drawRect:(CGRect)rect { if ([self hasText]) return; rect.origin.y += ( + );
rect.origin.x += ;
rect.size.width -= * rect.origin.x; NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = self.placeholdColor;
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
[self.placehold drawInRect:rect withAttributes:attrs];
} // 设计框架需注意,给外界提供了属性后,一定重写出行的setter,这样既可以时时监听使用者对属性的更改,还可以跟好的与外界代码进行交互
- (void)setPlacehold:(NSString *)placehold
{
_placehold = placehold;
// 设置了站位文字后,需要重绘一遍
[self setNeedsDisplay];
} - (void)setPlaceholdColor:(UIColor *)placeholdColor
{
_placeholdColor = placeholdColor;
[self setNeedsDisplay];
} // 同时,也要考虑到
- (void)setFont:(UIFont *)font
{
[super setFont:font]; [self setNeedsDisplay];
} - (void)setText:(NSString *)text
{
[super setText:text]; [self setNeedsDisplay];
} - (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText]; [self setNeedsDisplay];
} @end
  • 缺点:由于是画上去的,当设置了textView的alwaysBounceHorizontal属性后,不能跟随着scrollView的滑动而滑动,如下图

方案二:通过给TextView添加一个Label,也是通过监听文字改变的通知,修改label的hidden属性.

 #import "ChaosPlaceholdTextView.h"

 @interface ChaosPlaceholdTextView()
/** 占位文字label */
@property (nonatomic, weak) UILabel *placeholderLabel;
@end @implementation ChaosPlaceholdTextView - (UILabel *)placeholderLabel
{
if (!_placeholderLabel) {
// 添加一个用来显示占位文字的label
UILabel *placeholderLabel = [[UILabel alloc] init];
placeholderLabel.numberOfLines = ;
placeholderLabel.x = ;
placeholderLabel.y = ;
[self addSubview:placeholderLabel];
_placeholderLabel = placeholderLabel;
}
return _placeholderLabel;
} - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 垂直方向上永远有弹簧效果
self.alwaysBounceVertical = YES; // 默认字体
self.font = [UIFont systemFontOfSize:]; // 默认的占位文字颜色
self.placeholderColor = [UIColor grayColor]; // 监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} /**
* 监听文字改变
*/
- (void)textDidChange
{
// 只要有文字, 就隐藏占位文字label
self.placeholderLabel.hidden = self.hasText;
} /**
* 更新占位文字的尺寸
*/
- (void)updatePlaceholderLabelSize
{
CGSize maxSize = CGSizeMake(ChaosScreenW - * self.placeholderLabel.x, MAXFLOAT);
self.placeholderLabel.size = [self.placeholder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size;
} #pragma mark - 重写setter
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor; self.placeholderLabel.textColor = placeholderColor;
} - (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy]; self.placeholderLabel.text = placeholder; [self updatePlaceholderLabelSize];
} - (void)setFont:(UIFont *)font
{
[super setFont:font]; self.placeholderLabel.font = font; [self updatePlaceholderLabelSize];
} - (void)setText:(NSString *)text
{
[super setText:text]; [self textDidChange];
} - (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText]; [self textDidChange];
} @end

最新文章

  1. [BZOJ2391]Cirno的忧郁
  2. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q52-Q55)
  3. Maven dependency spring-web vs spring-webmvc
  4. linux 的终端字体色和背景色的修改方法(二)
  5. DOS下删除整个目录及下属所有文件夹及文件最好用的命令
  6. NET MVC权限验证
  7. mysql进阶(八)怎么对varchar类型排序问题
  8. Linux命令 ls 和 ll 的使用方法与基本区别
  9. expdp 字符集从ZHS16GBK到AL32UTF8
  10. Ex 6_1 和最大的相连子序列..._第五次作业
  11. 厉害了,他用PS不是P照片而是……
  12. Android Toolbar的使用 顶部标题栏+后退键
  13. tryparse
  14. 关于FSMC地址线的理解
  15. 两个提高工作效率的神器-Restlet Client和fe助手
  16. java单例模式等一些程序的写法....持续更新...
  17. 删除none的images
  18. D3基础---简介和数据
  19. SQL Server ->> 关于SQL Server Agent Job执行步骤时的用户上下文(User Context)问题
  20. javascript 时间日期处理相加相减

热门文章

  1. unix automake 使用,快速生成你的Makefile
  2. [No000001]一切都是最好的安排
  3. 直接拿来用!最火的Android开源项目(完结篇)
  4. JavaScript Array 对象
  5. swift三方库
  6. 浅析jQuery删除节点的三个方法
  7. 目录结构-内置(AJAX)帮助文档
  8. How do I list the files in a directory?
  9. nginx图片处理相关
  10. express:webpack dev-server开发中如何调用后端服务器的接口?