// // YOUPAILZTextView.m // VQU // // Created by CY on 2021/10/29. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAILZTextView.h" @interface YOUPAILZTextView() @property (nonatomic,assign)NSInteger youpaipmaxCount; @end @implementation YOUPAILZTextView - (instancetype)initWithMaxCount:(NSInteger)maxCount{ if (self = [super init]) { self.youpaipmaxCount = maxCount; [self youpaifinitValue]; [self youpaifinitUI]; } return self; } - (void)youpaifinitValue{ self.youpaiptextViewPadding = UIEdgeInsetsMake(16.0f, 16.0f, 16.0f, 16.0f); self.youpaiptextLengthDefaultColor = HexColorFromRGB(0x6C6B70); self.youpaiptextLengthHigtColor = HexColorFromRGB(0xF4003F); self.youpaiptextLengthFont = LCFont(14.0f); self.youpaiptextLengthRight = 16.0f; self.youpaiptextLengthBottom = 16.0f; } - (void)youpaifinitUI{ YYTextView *textView = [[YYTextView alloc] init]; textView.delegate = self; [self addSubview:textView]; self.youpaiptextView = textView; UILabel *youpaiptextLengthL = [[UILabel alloc] init]; youpaiptextLengthL.text = [NSString stringWithFormat:@"%@/%@",@(0),@(self.youpaipmaxCount)]; [self addSubview:youpaiptextLengthL]; self.youpaiptextLengthL = youpaiptextLengthL; } - (void)textViewDidChange:(YYTextView *)textView{ if (textView.text.length >= self.youpaipmaxCount) { self.youpaiptextLengthL.textColor = self.youpaiptextLengthHigtColor; self.youpaiptextView.text = [self.youpaiptextView.text substringToIndex:self.youpaipmaxCount]; }else{ self.youpaiptextLengthL.textColor = self.youpaiptextLengthDefaultColor; } self.youpaiptextLengthL.text = [NSString stringWithFormat:@"%@/%@",@(self.youpaiptextView.text.length),@(self.youpaipmaxCount)]; } - (void)layoutSubviews{ [super layoutSubviews]; self.youpaiptextLengthL.textColor = self.youpaiptextLengthDefaultColor; self.youpaiptextLengthL.font = self.youpaiptextLengthFont; [self.youpaiptextLengthL mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.offset(-self.youpaiptextLengthBottom); make.right.offset(-self.youpaiptextLengthRight); make.height.offset(self.youpaiptextLengthFont.pointSize + 2.0f); }]; [self.youpaiptextView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(self.youpaiptextViewPadding.left); make.right.offset(-self.youpaiptextViewPadding.right); make.top.offset(self.youpaiptextViewPadding.top); make.bottom.equalTo(self.youpaiptextLengthL.mas_top).offset(-self.youpaiptextViewPadding.bottom); }]; } @end