UIPlaceHolderTextView.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // UITextView+PlaceHolder.m
  3. // iLunch
  4. //
  5. // Created by AlexiChen on 15-1-11.
  6. // Copyright (c) 2015年 AlexiChen Chen. All rights reserved.
  7. //
  8. #import "UIPlaceHolderTextView.h"
  9. #import "NSString+Common.h"
  10. #import "BGHUDHelper.h"
  11. #import "UIView+Layout.h"
  12. @implementation UIPlaceHolderTextView
  13. - (void)dealloc
  14. {
  15. [self removeobserver];
  16. }
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. if ((self = [super initWithFrame:frame]))
  20. {
  21. [self addObserver];
  22. _placeHolderColor = kLightGrayColor;
  23. _mainTextColor = kBlackColor;
  24. }
  25. return self;
  26. }
  27. - (void)addObserver
  28. {
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginOfEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endOfEditing:) name:UITextViewTextDidEndEditingNotification object:self];
  31. }
  32. - (void)removeobserver
  33. {
  34. [[NSNotificationCenter defaultCenter] removeObserver:self];
  35. }
  36. #pragma mark -
  37. #pragma mark Setter/Getters
  38. - (void)setPlaceHolder:(NSString *)placeHolder
  39. {
  40. NSString *text = self.text;
  41. if ([NSString isEmpty:text] || [text isEqualToString:_placeHolder])
  42. {
  43. super.text = placeHolder;
  44. //注释颜色
  45. [super setTextColor:_placeHolderColor];
  46. }
  47. _placeHolder = placeHolder;
  48. }
  49. - (NSString *)text
  50. {
  51. NSString *text = [super text];
  52. if ([text isEqualToString:_placeHolder])
  53. {
  54. return nil;
  55. }
  56. return text;
  57. }
  58. - (void)beginOfEditing:(NSNotification *)notification
  59. {
  60. if ([super.text isEqualToString:_placeHolder])
  61. {
  62. super.text = nil;
  63. //字体颜色
  64. [super setTextColor:_mainTextColor];
  65. }
  66. // else
  67. // {
  68. // [super setTextColor:kBlackColor];
  69. // }
  70. }
  71. - (void)endOfEditing:(NSNotification *)notification
  72. {
  73. if ([NSString isEmpty:self.text])
  74. {
  75. super.text = _placeHolder;
  76. //注释颜色
  77. [super setTextColor:_placeHolderColor];
  78. }
  79. }
  80. @end
  81. @implementation UILimitTextView
  82. - (void)setLimitLength:(NSInteger)limitLength
  83. {
  84. if (!_limitText)
  85. {
  86. _limitText = [[UILabel alloc] init];
  87. _limitText.backgroundColor = kClearColor;
  88. _limitText.font = kAppSmallTextFont;
  89. _limitText.textColor = kLightGrayColor;
  90. _limitText.textAlignment = NSTextAlignmentCenter;
  91. [self addSubview:_limitText];
  92. }
  93. if (limitLength > 0)
  94. {
  95. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextViewEditChanged:) name:@"UITextViewTextDidChangeNotification" object:nil];
  96. }
  97. else
  98. {
  99. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextViewTextDidChangeNotification" object:nil];
  100. }
  101. _limitLength = limitLength;
  102. [self updateLimitText];
  103. }
  104. // 监听字符变化,并处理
  105. - (void)onTextViewEditChanged:(NSNotification *)obj
  106. {
  107. if (_limitLength > 0)
  108. {
  109. UITextView *textField = self;
  110. NSString *toBeString = textField.text;
  111. //获取高亮部分
  112. UITextRange *selectedRange = [textField markedTextRange];
  113. UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
  114. // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
  115. if (!position)
  116. {
  117. if (toBeString.length > _limitLength)
  118. {
  119. [textField shake];
  120. NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:_limitLength];
  121. if (rangeIndex.length == 1)
  122. {
  123. textField.text = [toBeString substringToIndex:_limitLength];
  124. }
  125. else
  126. {
  127. NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, _limitLength)];
  128. textField.text = [toBeString substringWithRange:rangeRange];
  129. }
  130. }
  131. }
  132. }
  133. }
  134. - (BOOL)shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  135. {
  136. if (range.length == 0)
  137. {
  138. // 表求增加
  139. if (self.text.length + text.length > _limitLength)
  140. {
  141. [_limitText shake];
  142. return NO;
  143. }
  144. }
  145. return YES;
  146. }
  147. - (void)updateLimitText
  148. {
  149. NSInteger curLength = self.text.length;
  150. if (curLength > _limitLength)
  151. {
  152. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"已达到最大限制字数")];
  153. _limitText.text = [NSString stringWithFormat:@"%ld/%ld", (long)_limitLength, (long)_limitLength];
  154. }
  155. else
  156. {
  157. _limitText.text = [NSString stringWithFormat:@"%ld/%ld", (long)curLength, (long)_limitLength];
  158. }
  159. [self setNeedsLayout];
  160. }
  161. - (void)layoutSubviews
  162. {
  163. [super layoutSubviews];
  164. if (_limitText)
  165. {
  166. CGSize contentSize = self.contentSize;
  167. if (contentSize.height < self.bounds.size.height)
  168. {
  169. contentSize = self.bounds.size;
  170. }
  171. else
  172. {
  173. // 处理contentoffset的问题
  174. }
  175. CGSize textSize = [_limitText textSizeIn:contentSize];
  176. CGRect rect = CGRectMake(0, 0, textSize.width + kDefaultMargin, textSize.height + kDefaultMargin);
  177. rect.origin.x += contentSize.width - rect.size.width - kDefaultMargin;
  178. rect.origin.y += contentSize.height - rect.size.height - kDefaultMargin;
  179. _limitText.frame = rect;
  180. }
  181. }
  182. @end