ZCLiveTextView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // TLSLiveTextView.m
  3. // tls
  4. //
  5. // Created by 张灿 on 2017/4/24.
  6. // Copyright © 2017年 tianlishe. All rights reserved.
  7. //
  8. #import "ZCLiveTextView.h"
  9. @interface ZCLiveTextView()<UITextViewDelegate>
  10. @property (nonatomic, weak) UILabel *placeholderLabel;
  11. @property(nonatomic,assign) CGFloat topMargin;
  12. @property(nonatomic,assign) CGFloat leftMargin;
  13. @property (nonatomic, weak) UILabel *alertLabel;
  14. @end
  15. @implementation ZCLiveTextView
  16. - (instancetype)initWithFrame:(CGRect)frame TopMargin:(CGFloat)topMargin LeftMargin:(CGFloat)leftMargin
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. // 1.添加提示文字
  21. _topMargin = topMargin;
  22. _leftMargin = leftMargin;
  23. _timeoutAlertColor = [UIColor redColor];
  24. self.backgroundColor = [UIColor whiteColor];
  25. UITextView* textview = [[UITextView alloc]init];
  26. textview.tintColor = ZYPinkColor;
  27. textview.backgroundColor = [UIColor clearColor];
  28. textview.font = [UIFont systemFontOfSize:15];
  29. textview.textColor = HexColorFromRGB(0x333333);
  30. textview.delegate = self;
  31. [self addSubview:textview];
  32. self.textView = textview;
  33. textview.frame = CGRectMake(leftMargin-3,3,self.frame.size.width-2*leftMargin+6, self.frame.size.height-20);
  34. //placeHolder
  35. UILabel *placeholderLabel = [[UILabel alloc] init];
  36. placeholderLabel.numberOfLines = 0;
  37. placeholderLabel.textColor = HexColorFromRGB(0xb5b5b5);
  38. placeholderLabel.hidden = YES;
  39. placeholderLabel.numberOfLines = 0;
  40. placeholderLabel.backgroundColor = [UIColor clearColor];
  41. placeholderLabel.font = LCFont14;
  42. self.placeholderLabel = placeholderLabel;
  43. [self addSubview:placeholderLabel];
  44. //alertLabel
  45. UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-leftMargin-200, self.frame.size.height-leftMargin-15, 200, 15)];
  46. alertLabel.textColor = HexColorFromRGB(0x6C6B70);
  47. alertLabel.textAlignment = NSTextAlignmentRight;
  48. alertLabel.numberOfLines = 1;
  49. alertLabel.backgroundColor = [UIColor clearColor];
  50. alertLabel.font = LCFont14;
  51. self.alertLabel = alertLabel;
  52. [self addSubview:alertLabel];
  53. // 2.监听textView文字改变的通知
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self.textView];
  55. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidBeginEditingNotification object:self.textView];
  56. }
  57. return self;
  58. }
  59. - (void)setPlaceholder:(NSString *)placeholder
  60. {
  61. _placeholder = [placeholder copy];
  62. self.placeholderLabel.text = placeholder;
  63. if (placeholder.length) { // 需要显示
  64. self.placeholderLabel.hidden = NO;
  65. // 计算frame
  66. CGFloat placeholderX = _leftMargin;
  67. CGFloat placeholderY = _topMargin;
  68. CGFloat maxW = self.frame.size.width - 2 * placeholderX;
  69. CGFloat maxH = self.frame.size.height - 2 * placeholderY;
  70. CGSize placeholderSize= [placeholder boundingRectWithSize:CGSizeMake(maxW, maxH) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.placeholderLabel.font } context:nil].size;
  71. self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width+5, placeholderSize.height+5);
  72. } else {
  73. self.placeholderLabel.hidden = YES;
  74. }
  75. }
  76. - (void)setTopMargin:(CGFloat)topMargin{
  77. _topMargin = topMargin;
  78. }
  79. - (void)setLeftMargin:(CGFloat)leftMargin{
  80. _leftMargin = leftMargin;
  81. }
  82. - (void)setTimeoutAlertColor:(UIColor *)timeoutAlertColor{
  83. _timeoutAlertColor = timeoutAlertColor;
  84. }
  85. - (void)setTextLength:(NSInteger)textLength{
  86. _textLength = textLength;
  87. self.alertLabel.text = [NSString stringWithFormat:@"%ld/%zd",self.textView.text.length,_textLength];
  88. }
  89. - (void)setPlaceholderColor:(UIColor *)placeholderColor
  90. {
  91. _placeholderColor = placeholderColor;
  92. self.placeholderLabel.textColor = placeholderColor;
  93. }
  94. - (void)textDidChange
  95. {
  96. self.placeholderLabel.hidden = (self.textView.text.length != 0);
  97. if (self.textView.text.length<_textLength) {
  98. self.alertLabel.textColor = HexColorFromRGB(0x6C6B70);
  99. }else if(self.textView.text.length<=_textLength){
  100. self.alertLabel.textColor = _timeoutAlertColor;
  101. }else{
  102. self.textView.text = [self.textView.text substringToIndex:_textLength];
  103. }
  104. // self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  105. self.alertLabel.text = [NSString stringWithFormat:@"%ld/%zd",self.textView.text.length,_textLength];
  106. }
  107. - (void)textViewDidChangeSelection:(UITextView *)textView{
  108. [self textDidChange];
  109. }
  110. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
  111. // if ([text isEqualToString:@"\n"]) {
  112. // return NO;
  113. // }
  114. return YES;
  115. }
  116. - (void)dealloc
  117. {
  118. [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:self];
  119. [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self.textView];
  120. [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidBeginEditingNotification object:self.textView];
  121. }
  122. @end