123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- //
- // TLSLiveTextView.m
- // tls
- //
- // Created by 张灿 on 2017/4/24.
- // Copyright © 2017年 tianlishe. All rights reserved.
- //
- #import "ZCLiveTextView.h"
- @interface ZCLiveTextView()<UITextViewDelegate>
- @property (nonatomic, weak) UILabel *placeholderLabel;
- @property(nonatomic,assign) CGFloat topMargin;
- @property(nonatomic,assign) CGFloat leftMargin;
- @property (nonatomic, weak) UILabel *alertLabel;
- @end
- @implementation ZCLiveTextView
- - (instancetype)initWithFrame:(CGRect)frame TopMargin:(CGFloat)topMargin LeftMargin:(CGFloat)leftMargin
- {
- self = [super initWithFrame:frame];
- if (self) {
- // 1.添加提示文字
- _topMargin = topMargin;
- _leftMargin = leftMargin;
- _timeoutAlertColor = [UIColor redColor];
-
- self.backgroundColor = [UIColor whiteColor];
- UITextView* textview = [[UITextView alloc]init];
- textview.tintColor = ZYPinkColor;
- textview.backgroundColor = [UIColor clearColor];
- textview.font = [UIFont systemFontOfSize:15];
- textview.textColor = HexColorFromRGB(0x333333);
- textview.delegate = self;
- [self addSubview:textview];
- self.textView = textview;
- textview.frame = CGRectMake(leftMargin-3,3,self.frame.size.width-2*leftMargin+6, self.frame.size.height-20);
- //placeHolder
- UILabel *placeholderLabel = [[UILabel alloc] init];
- placeholderLabel.numberOfLines = 0;
- placeholderLabel.textColor = HexColorFromRGB(0xb5b5b5);
- placeholderLabel.hidden = YES;
- placeholderLabel.numberOfLines = 0;
- placeholderLabel.backgroundColor = [UIColor clearColor];
- placeholderLabel.font = LCFont14;
-
- self.placeholderLabel = placeholderLabel;
- [self addSubview:placeholderLabel];
- //alertLabel
- UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-leftMargin-200, self.frame.size.height-leftMargin-15, 200, 15)];
- alertLabel.textColor = HexColorFromRGB(0x6C6B70);
- alertLabel.textAlignment = NSTextAlignmentRight;
- alertLabel.numberOfLines = 1;
- alertLabel.backgroundColor = [UIColor clearColor];
- alertLabel.font = LCFont14;
- self.alertLabel = alertLabel;
- [self addSubview:alertLabel];
- // 2.监听textView文字改变的通知
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self.textView];
-
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidBeginEditingNotification object:self.textView];
- }
- return self;
- }
- - (void)setPlaceholder:(NSString *)placeholder
- {
- _placeholder = [placeholder copy];
-
- self.placeholderLabel.text = placeholder;
- if (placeholder.length) { // 需要显示
- self.placeholderLabel.hidden = NO;
-
- // 计算frame
- CGFloat placeholderX = _leftMargin;
- CGFloat placeholderY = _topMargin;
- CGFloat maxW = self.frame.size.width - 2 * placeholderX;
- CGFloat maxH = self.frame.size.height - 2 * placeholderY;
- CGSize placeholderSize= [placeholder boundingRectWithSize:CGSizeMake(maxW, maxH) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.placeholderLabel.font } context:nil].size;
- self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width+5, placeholderSize.height+5);
- } else {
- self.placeholderLabel.hidden = YES;
- }
- }
- - (void)setTopMargin:(CGFloat)topMargin{
- _topMargin = topMargin;
- }
- - (void)setLeftMargin:(CGFloat)leftMargin{
- _leftMargin = leftMargin;
- }
- - (void)setTimeoutAlertColor:(UIColor *)timeoutAlertColor{
- _timeoutAlertColor = timeoutAlertColor;
- }
- - (void)setTextLength:(NSInteger)textLength{
- _textLength = textLength;
- self.alertLabel.text = [NSString stringWithFormat:@"%ld/%zd",self.textView.text.length,_textLength];
- }
- - (void)setPlaceholderColor:(UIColor *)placeholderColor
- {
- _placeholderColor = placeholderColor;
-
- self.placeholderLabel.textColor = placeholderColor;
- }
- - (void)textDidChange
- {
-
- self.placeholderLabel.hidden = (self.textView.text.length != 0);
- if (self.textView.text.length<_textLength) {
- self.alertLabel.textColor = HexColorFromRGB(0x6C6B70);
- }else if(self.textView.text.length<=_textLength){
-
- self.alertLabel.textColor = _timeoutAlertColor;
- }else{
-
- self.textView.text = [self.textView.text substringToIndex:_textLength];
- }
- // self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- self.alertLabel.text = [NSString stringWithFormat:@"%ld/%zd",self.textView.text.length,_textLength];
-
- }
- - (void)textViewDidChangeSelection:(UITextView *)textView{
- [self textDidChange];
- }
- -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
- // if ([text isEqualToString:@"\n"]) {
- // return NO;
- // }
- return YES;
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:self];
- [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self.textView];
- [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidBeginEditingNotification object:self.textView];
- }
- @end
|