iOS开发小技巧--自定义带有占位文字的TextView(两种方式)
2024-10-13 11:03:07
自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写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
最新文章
- [BZOJ2391]Cirno的忧郁
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q52-Q55)
- Maven dependency spring-web vs spring-webmvc
- linux 的终端字体色和背景色的修改方法(二)
- DOS下删除整个目录及下属所有文件夹及文件最好用的命令
- NET MVC权限验证
- mysql进阶(八)怎么对varchar类型排序问题
- Linux命令 ls 和 ll 的使用方法与基本区别
- expdp 字符集从ZHS16GBK到AL32UTF8
- Ex 6_1 和最大的相连子序列..._第五次作业
- 厉害了,他用PS不是P照片而是……
- Android Toolbar的使用 顶部标题栏+后退键
- tryparse
- 关于FSMC地址线的理解
- 两个提高工作效率的神器-Restlet Client和fe助手
- java单例模式等一些程序的写法....持续更新...
- 删除none的images
- D3基础---简介和数据
- SQL Server ->;>; 关于SQL Server Agent Job执行步骤时的用户上下文(User Context)问题
- javascript 时间日期处理相加相减