@interface YCAnimationTextField : UIView
//注释文字正常情况颜色
@property (nonatomic, strong) UIColor *placeHolderNormalTextColor;
//注释文字选中情况颜色
@property (nonatomic, strong) UIColor *placeHolderSelectTextColor;
//光标颜色
@property (nonatomic, strong) UIColor *cursorColor;
//注释文字
@property (nonatomic, copy) NSString *placeHolder;
@end
#import "YCAnimationTextField.h"
#define kSelfWidth CGRectGetWidth(self.frame)
#define kSelfHeight CGRectGetHeight(self.frame)
const CGFloat lineWidth = 1.f;
const CGFloat duration = .2f;
@interface YCAnimationTextField ()
@property (nonatomic, strong) UILabel *placeHolderLb;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) CALayer *bottomLayer;
@property (nonatomic, assign) BOOL moved;
@end
@implementation YCAnimationTextField
@synthesize placeHolderNormalTextColor = _placeHolderNormalTextColor;
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
_placeHolderLb = [UILabel new];
_placeHolderLb.font = [UIFont systemFontOfSize:15];
_placeHolderLb.textColor = self.placeHolderNormalTextColor;
[self addSubview:_placeHolderLb];
_textField = [UITextField new];
_textField.backgroundColor = [UIColor clearColor];
_textField.textColor = [UIColor whiteColor];
_textField.font = [UIFont systemFontOfSize:15];
//光标颜色
_textField.tintColor = [UIColor whiteColor];
_textField.borderStyle = UITextBorderStyleNone;
[self addSubview:_textField];
_bottomLine = [UIView new];
_bottomLine.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_bottomLine];
_bottomLayer = [CALayer layer];
_bottomLayer.backgroundColor = [UIColor whiteColor].CGColor;
_bottomLayer.anchorPoint = CGPointMake(0, 0.5);
_bottomLayer.frame = CGRectMake(0, 0, 0, lineWidth);
//直接添加到_bottomLine.layer中,不要添加到self.layer
[_bottomLine.layer addSublayer:_bottomLayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(valueDidChanged:) name:UITextFieldTextDidChangeNotification object:_textField];
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
_placeHolderLb.frame = CGRectMake(0, 0, kSelfWidth, kSelfHeight-lineWidth);
_bottomLine.frame = CGRectMake(0, kSelfHeight-lineWidth, kSelfWidth, lineWidth);
_textField.frame = CGRectMake(0, 0, kSelfWidth, kSelfHeight-lineWidth);
}
- (void)valueDidChanged:(NSNotification *)notify{
[self placeHolderFrameChange];
}
- (void)placeHolderFrameChange{
CGFloat x = _placeHolderLb.center.x;
CGFloat y = _placeHolderLb.center.y;
if (_textField.text.length>0 && !_moved) {
[self moveAnimationWithX:x andY:y];
}else if (_textField.text.length == 0 && _moved){
[self backAnimationWithX:x andY:y];
}
}
- (void)moveAnimationWithX:(CGFloat)x andY:(CGFloat)y{
_moved = YES;
_placeHolderLb.textColor = _placeHolderSelectTextColor;
x -= 2;
y -= _placeHolderLb.frame.size.height/2-3;
[UIView animateWithDuration:duration animations:^{
_placeHolderLb.font = [UIFont systemFontOfSize:11];
_bottomLayer.frame = CGRectMake(0, 0, kSelfWidth, lineWidth);
_placeHolderLb.center = CGPointMake(x, y);
_placeHolderLb.alpha =1;
}];
}
- (void)backAnimationWithX:(CGFloat)x andY:(CGFloat)y{
_moved = NO;
_placeHolderLb.textColor = self.placeHolderNormalTextColor;
x += 2;
y += _placeHolderLb.frame.size.height/2-3;
[UIView animateWithDuration:duration animations:^{
_placeHolderLb.font = [UIFont systemFontOfSize:15];
_bottomLayer.frame = CGRectMake(0, 0, 0, lineWidth);
_placeHolderLb.center = CGPointMake(x, y);
_placeHolderLb.alpha =1;
}];
}
- (void)setPlaceHolder:(NSString *)placeHolder{
_placeHolder = placeHolder;
_placeHolderLb.text = placeHolder;
}
- (void)setPlaceHolderNormalTextColor:(UIColor *)placeHolderNormalTextColor{
_placeHolderNormalTextColor = placeHolderNormalTextColor;
_placeHolderLb.textColor = placeHolderNormalTextColor;
}
- (UIColor *)placeHolderSelectTextColor{
if (!_placeHolderSelectTextColor) {
return [UIColor blackColor];
}else{
return _placeHolderSelectTextColor;
}
}
- (UIColor *)placeHolderNormalTextColor{
if (!_placeHolderNormalTextColor) {
return [UIColor blackColor];
}else{
return _placeHolderNormalTextColor;
}
}
- (void)setCursorColor:(UIColor *)cursorColor{
_cursorColor = cursorColor;
_textField.tintColor = cursorColor;
}
@end