YOUPAILZTextView.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // YOUPAILZTextView.m
  3. // VQU
  4. //
  5. // Created by CY on 2021/10/29.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAILZTextView.h"
  9. @interface YOUPAILZTextView()<YYTextViewDelegate>
  10. @property (nonatomic,assign)NSInteger youpaipmaxCount;
  11. @end
  12. @implementation YOUPAILZTextView
  13. - (instancetype)initWithMaxCount:(NSInteger)maxCount{
  14. if (self = [super init]) {
  15. self.youpaipmaxCount = maxCount;
  16. [self youpaifinitValue];
  17. [self youpaifinitUI];
  18. }
  19. return self;
  20. }
  21. - (void)youpaifinitValue{
  22. self.youpaiptextViewPadding = UIEdgeInsetsMake(16.0f, 16.0f, 16.0f, 16.0f);
  23. self.youpaiptextLengthDefaultColor = HexColorFromRGB(0x6C6B70);
  24. self.youpaiptextLengthHigtColor = HexColorFromRGB(0xF4003F);
  25. self.youpaiptextLengthFont = LCFont(14.0f);
  26. self.youpaiptextLengthRight = 16.0f;
  27. self.youpaiptextLengthBottom = 16.0f;
  28. }
  29. - (void)youpaifinitUI{
  30. YYTextView *textView = [[YYTextView alloc] init];
  31. textView.delegate = self;
  32. [self addSubview:textView];
  33. self.youpaiptextView = textView;
  34. UILabel *youpaiptextLengthL = [[UILabel alloc] init];
  35. youpaiptextLengthL.text = [NSString stringWithFormat:@"%@/%@",@(0),@(self.youpaipmaxCount)];
  36. [self addSubview:youpaiptextLengthL];
  37. self.youpaiptextLengthL = youpaiptextLengthL;
  38. }
  39. - (void)textViewDidChange:(YYTextView *)textView{
  40. if (textView.text.length >= self.youpaipmaxCount) {
  41. self.youpaiptextLengthL.textColor = self.youpaiptextLengthHigtColor;
  42. self.youpaiptextView.text = [self.youpaiptextView.text substringToIndex:self.youpaipmaxCount];
  43. }else{
  44. self.youpaiptextLengthL.textColor = self.youpaiptextLengthDefaultColor;
  45. }
  46. self.youpaiptextLengthL.text = [NSString stringWithFormat:@"%@/%@",@(self.youpaiptextView.text.length),@(self.youpaipmaxCount)];
  47. }
  48. - (void)layoutSubviews{
  49. [super layoutSubviews];
  50. self.youpaiptextLengthL.textColor = self.youpaiptextLengthDefaultColor;
  51. self.youpaiptextLengthL.font = self.youpaiptextLengthFont;
  52. [self.youpaiptextLengthL mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.bottom.offset(-self.youpaiptextLengthBottom);
  54. make.right.offset(-self.youpaiptextLengthRight);
  55. make.height.offset(self.youpaiptextLengthFont.pointSize + 2.0f);
  56. }];
  57. [self.youpaiptextView mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.left.offset(self.youpaiptextViewPadding.left);
  59. make.right.offset(-self.youpaiptextViewPadding.right);
  60. make.top.offset(self.youpaiptextViewPadding.top);
  61. make.bottom.equalTo(self.youpaiptextLengthL.mas_top).offset(-self.youpaiptextViewPadding.bottom);
  62. }];
  63. }
  64. @end