// // UIPlaceHolderTextView.m // xbxm // // Created by Scott Su on 14-12-6. // Copyright (c) 2014年 tongxueplus. All rights reserved. // #import "UIPlaceHolderTextView.h" @interface UIPlaceHolderTextView () @end @implementation UIPlaceHolderTextView CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25; - (instancetype)init{ self = [super init]; if (self) { if (!self.placeholder) { [self setPlaceholder:@""]; } if (!self.placeholderColor) { [self setPlaceholderColor:UIColorHex(#999999)]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; if (_placeHolderLabel == nil ) { _placeHolderLabel = [[UILabel alloc] init]; _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; _placeHolderLabel.numberOfLines = 0; _placeHolderLabel.font = self.font; _placeHolderLabel.backgroundColor = [UIColor clearColor]; _placeHolderLabel.textColor = self.placeholderColor; _placeHolderLabel.alpha = 0; _placeHolderLabel.tag = 999; [self addSubview:_placeHolderLabel]; } _placeHolderLabel.text = self.placeholder; [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.mas_offset(8); make.right.mas_offset(-8); make.height.mas_greaterThanOrEqualTo(20); }]; [self sendSubviewToBack:_placeHolderLabel]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; // Use Interface Builder User Defined Runtime Attributes to set // placeholder and placeholderColor in Interface Builder. if (!self.placeholder) { [self setPlaceholder:@""]; } if (!self.placeholderColor) { [self setPlaceholderColor:UIColorHex(#999999)]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]; if (_placeHolderLabel == nil ) { // _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)]; _placeHolderLabel = [[UILabel alloc] init]; _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; _placeHolderLabel.numberOfLines = 0; _placeHolderLabel.font = self.font; _placeHolderLabel.backgroundColor = [UIColor clearColor]; _placeHolderLabel.textColor = self.placeholderColor; _placeHolderLabel.alpha = 0; _placeHolderLabel.tag = 999; // _placeHolderLabel.backgroundColor = RGB(51, 51, 51); [self addSubview:_placeHolderLabel]; } _placeHolderLabel.text = self.placeholder; // [_placeHolderLabel sizeToFit]; // CGFloat H = [self.placeholder sizeWithFont:self.font byWidth:self.bounds.size.width - 16].height; [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.mas_offset(8); make.right.mas_offset(-8); make.height.mas_greaterThanOrEqualTo(20); }]; // _placeHolderLabel.frame = CGRectMake(8, 8, self.bounds.size.width - 16, [self.placeholder sizeWithFont:self.font byWidth:self.bounds.size.width - 16].height); [self sendSubviewToBack:_placeHolderLabel]; } - (void)setPlaceholder:(NSString *)placeholder { _placeholder = placeholder; _placeHolderLabel.text = _placeholder; } - (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)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)textChanged:(NSNotification *)notification { if([[self placeholder] length] == 0) { return; } [self.placeHolderLabel setText:self.placeholder]; [UIView animateWithDuration:0.2 animations:^{ if([[self text] length] == 0) { [(UILabel *)[self viewWithTag:999] setAlpha:1]; } else { [(UILabel *)[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( [[self text] length] == 0 && [[self placeholder] length] > 0 ) { [(UILabel *)[self viewWithTag:999] setAlpha:1]; } [super drawRect:rect]; } @end