JHHolderTextView.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // JHHolderTextView.m
  3. // JHKit
  4. //
  5. // Created by HaoCold on 2017/4/1.
  6. // Copyright © 2017年 HaoCold. All rights reserved.
  7. //
  8. // MIT License
  9. //
  10. // Copyright (c) 2017 xjh093
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining a copy
  13. // of this software and associated documentation files (the "Software"), to deal
  14. // in the Software without restriction, including without limitation the rights
  15. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. // copies of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be included in all
  20. // copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. // SOFTWARE.
  29. #import "JHHolderTextView.h"
  30. @interface JHHolderTextView()<UITextViewDelegate>
  31. @property (nonatomic, strong) UILabel *holderLabel;
  32. @property (nonatomic, strong) UILabel *limitedLabel;
  33. @end
  34. @implementation JHHolderTextView
  35. - (instancetype)initWithFrame:(CGRect)frame
  36. {
  37. self = [super initWithFrame:frame];
  38. if (self) {
  39. _limitCountPrefixText = @"";
  40. _limitCountSubfixText = @"";
  41. _showLimitCount = YES;
  42. [self jhSetupViews:frame];
  43. }
  44. return self;
  45. }
  46. - (void)jhSetupViews:(CGRect)frame
  47. {
  48. [self addSubview:({[[UIView alloc] init];})];
  49. CGFloat X = 10;
  50. CGFloat Y = 5;
  51. UITextView *textView = [[UITextView alloc] init];
  52. textView.frame = CGRectMake(X, Y, frame.size.width-2*X, frame.size.height-2*Y-16);
  53. textView.font = [UIFont systemFontOfSize:14];
  54. textView.delegate = self;
  55. textView.showsVerticalScrollIndicator = NO;
  56. [self addSubview:textView];
  57. _textView = textView;
  58. UILabel *holderLabel = [[UILabel alloc] init];
  59. holderLabel.frame = CGRectMake(X, textView.frame.origin.y+10, frame.size.width-2*X, 14);
  60. holderLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
  61. holderLabel.font = textView.font;
  62. // holderLabel.text = @"填写\"震\"内容或点击\"确定\"直接震";
  63. [self addSubview:holderLabel];
  64. _holderLabel = holderLabel;
  65. UILabel *limitedLabel = [[UILabel alloc] init];
  66. limitedLabel.frame = CGRectMake(X, CGRectGetMaxY(textView.frame), CGRectGetWidth(textView.frame), 16);
  67. limitedLabel.text = @"";
  68. limitedLabel.textColor = [UIColor blackColor];
  69. limitedLabel.font = [UIFont systemFontOfSize:14];
  70. limitedLabel.textAlignment = 2;
  71. [self addSubview:limitedLabel];
  72. _limitedLabel = limitedLabel;
  73. self.layer.borderWidth = 0.5;
  74. self.layer.borderColor = [UIColor lightGrayColor].CGColor;
  75. }
  76. #pragma mark - UITextViewDelegate
  77. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  78. {
  79. // _holderLabel.hidden = YES;
  80. return YES;
  81. }
  82. - (void)textViewDidChange:(UITextView *)textView
  83. {
  84. [self jhCount:textView];
  85. }
  86. - (void)textViewDidEndEditing:(UITextView *)textView
  87. {
  88. [self jhCount:textView];
  89. }
  90. - (void)jhCount:(UITextView *)textView
  91. {
  92. if (textView.text.length == 0) {
  93. _holderLabel.hidden = NO;
  94. }else{
  95. _holderLabel.hidden = YES;
  96. }
  97. if (_limitCount == 0) {
  98. return;
  99. }
  100. if (textView.text.length > _limitCount) {
  101. textView.text = [textView.text substringToIndex:_limitCount];
  102. _limitedLabel.text = [NSString stringWithFormat:@"%@%@/%@%@",_limitCountPrefixText,@(_limitCount),@(_limitCount),_limitCountSubfixText];
  103. }else{
  104. _limitedLabel.text = [NSString stringWithFormat:@"%@%@/%@%@",_limitCountPrefixText,@(textView.text.length),@(_limitCount),_limitCountSubfixText];
  105. }
  106. }
  107. - (void)setText:(NSString *)text{
  108. if (text) {
  109. _textView.text = text;
  110. [self jhCount:_textView];
  111. }
  112. }
  113. - (NSString *)text{
  114. return _textView.text;
  115. }
  116. - (void)setTextColor:(UIColor *)textColor{
  117. _textColor = textColor;
  118. _textView.textColor = textColor;
  119. }
  120. - (void)setFont:(UIFont *)font{
  121. _font = font;
  122. _textView.font = font;
  123. _holderLabel.font = font;
  124. }
  125. - (void)setHolder:(NSString *)holder{
  126. _holderLabel.text = [NSString stringWithFormat:@" %@",holder];
  127. }
  128. - (NSString *)holder{
  129. return _holderLabel.text;
  130. }
  131. - (void)setHolderTextColor:(UIColor *)holderTextColor{
  132. _holderTextColor = holderTextColor;
  133. _holderLabel.textColor = holderTextColor;
  134. }
  135. - (void)setHolderFont:(UIFont *)holderFont{
  136. _holderFont = holderFont;
  137. _holderLabel.font = holderFont;
  138. }
  139. - (void)setLimitCount:(NSUInteger)limitCount{
  140. _limitCount = limitCount;
  141. _limitedLabel.text = [NSString stringWithFormat:@"%@0/%@",_limitCountPrefixText,@(limitCount)];
  142. [self jhCount:_textView];
  143. }
  144. - (void)setLimitCountTextColor:(UIColor *)limitCountTextColor{
  145. _limitCountTextColor = limitCountTextColor;
  146. _limitedLabel.textColor = limitCountTextColor;
  147. }
  148. - (void)setLimitCountFont:(UIFont *)limitCountFont{
  149. _limitCountFont = limitCountFont;
  150. _limitedLabel.font = limitCountFont;
  151. }
  152. - (void)setLimitCountPrefixText:(NSString *)limitCountPrefixText{
  153. _limitCountPrefixText = limitCountPrefixText;
  154. _limitedLabel.text = [NSString stringWithFormat:@"%@0/%@%@",limitCountPrefixText,@(_limitCount),_limitCountSubfixText];
  155. }
  156. - (void)setLimitCountSubfixText:(NSString *)limitCountSubfixText{
  157. _limitCountSubfixText = limitCountSubfixText;
  158. _limitedLabel.text = [NSString stringWithFormat:@"%@0/%@%@",_limitCountPrefixText,@(_limitCount),limitCountSubfixText];
  159. }
  160. - (void)setLimitCountOffsetX:(CGFloat)limitCountOffsetX{
  161. _limitCountOffsetX = limitCountOffsetX;
  162. _limitedLabel.center = CGPointMake(_limitedLabel.center.x + limitCountOffsetX, _limitedLabel.center.y);
  163. }
  164. - (void)setLimitCountOffsetY:(CGFloat)limitCountOffsetY{
  165. _limitCountOffsetY = limitCountOffsetY;
  166. _limitedLabel.center = CGPointMake(_limitedLabel.center.x, _limitedLabel.center.y + limitCountOffsetY);
  167. }
  168. - (void)setShowLimitCount:(BOOL)showLimitCount{
  169. if (_showLimitCount != showLimitCount) {
  170. _showLimitCount = showLimitCount;
  171. CGRect frame = _textView.frame;
  172. if (_showLimitCount) {
  173. frame.size.height -= 16;
  174. }else{
  175. frame.size.height += 16;
  176. }
  177. _textView.frame = frame;
  178. _limitedLabel.hidden = !showLimitCount;
  179. }
  180. }
  181. @end