用惯TextField的placeholder属性,当然在使用TextView自然而然 .placeholder可这时怎么点都点不出来,
下面废话不多说,直接介绍可以使用TextView. placeholder的方法
创建自定义placeHolderTextView类继承于UITextView
- 在placeHolderTextView.h 中
#import <UIKit/UIKit.h>
@interface placeHolderTextView : UITextView
@property (nonatomic, retain) NSString *placeholder;
@property (nonatomic, retain) UIColor *placeholderColor;
-(void)textChanged:(NSNotification*)notification;
@end
- 在placeHolderTextView.m中
#import "placeHolderTextView.h"
@interface placeHolderTextView ()
@property (nonatomic, retain) UILabel *placeHolderLabel;
@end
@implementation placeHolderTextView
CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)awakeFromNib {
[super awakeFromNib];
if (!self.placeholder) {
[self setPlaceholder:@""];
}
if (!self.placeholderColor) {
[self setPlaceholderColor:[UIColor lightGrayColor]];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}
- (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;
}
[UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
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-5,10)];
_placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
_placeHolderLabel.numberOfLines =0;
// _placeHolderLabel.font = self.font; //调整textView字体
_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
- 使用步骤
一. 导入头文件 #import "placeHolderTextView.h"
二. 定义属性
@property(strong,nonatomic) placeHolderTextView *textView;
三. 创建设置添加图层容器
self.textView=[[cnhbPlaceHolderTextView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_W-20, 110)];
self.textView.placeholder=@" 描述 xxxxxxxxxxxxxxxx 信息";
self.textView.layer.borderColor=[UIColor lightGrayColor].CGColor;
self.textView.layer.borderWidth=1.0;
self.textView.scrollEnabled = YES;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; //自适应高度
self.textView.returnKeyType = UIReturnKeyDefault; //返回键的类型
self.textView.keyboardType = UIKeyboardTypeDefault; //键盘类型
[self.view addSubview:self.textView];
我是楚简约,感谢您的阅读,
喜欢就点个赞呗,“❤喜欢”,
鼓励又不花钱,你在看,我就继续写~
非简书用户,可以点右上角的三个“...”,然后"在Safari中打开”,就可以点赞咯~
到此为止.记录下容易忘的细节同大家分享!!!🙂🙂🙂