No Description

BRPlaceholderTextView.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // PlaceholderTextView.m
  3. // SaleHelper
  4. //
  5. // Created by gitBurning on 14/12/8.
  6. // Copyright (c) 2014年 Burning_git. All rights reserved.
  7. //
  8. #import "BRPlaceholderTextView.h"
  9. #define kTopY 14.0
  10. #define kLeftX 13.0
  11. @interface BRPlaceholderTextView()<UITextViewDelegate>
  12. @property(strong,nonatomic) UIColor *placeholder_color;
  13. @property(strong,nonatomic) UIFont * placeholder_font;
  14. /**
  15. * 显示 Placeholder
  16. */
  17. @property(strong,nonatomic,readonly) UILabel *PlaceholderLabel;
  18. @property(assign,nonatomic) float placeholdeWidth;
  19. @property(copy,nonatomic) id eventBlock;
  20. @property(copy,nonatomic) id BeginBlock;
  21. @property(copy,nonatomic) id EndBlock;
  22. @end
  23. @implementation BRPlaceholderTextView
  24. // Only override drawRect: if you perform custom drawing.
  25. // An empty implementation adversely affects performance during animation.
  26. #pragma mark - life cycle
  27. - (id) initWithFrame:(CGRect)frame {
  28. if ((self = [super initWithFrame:frame])) {
  29. [self awakeFromNib];
  30. }
  31. return self;
  32. }
  33. - (void)awakeFromNib {
  34. [super awakeFromNib];
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidChange:) name:UITextViewTextDidChangeNotification object:self];
  36. //UITextViewTextDidBeginEditingNotification
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewBeginNoti:) name:UITextViewTextDidBeginEditingNotification object:self];
  38. //UITextViewTextDidEndEditingNotification
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEndNoti:) name:UITextViewTextDidEndEditingNotification object:self];
  40. float left=kLeftX,top=kTopY,hegiht=30;
  41. self.placeholdeWidth=CGRectGetWidth(self.frame)-2*left;
  42. _PlaceholderLabel=[[UILabel alloc] initWithFrame:CGRectMake(left, top
  43. , _placeholdeWidth, hegiht)];
  44. _PlaceholderLabel.numberOfLines=0;
  45. _PlaceholderLabel.lineBreakMode=NSLineBreakByCharWrapping|NSLineBreakByWordWrapping;
  46. [self addSubview:_PlaceholderLabel];
  47. [self defaultConfig];
  48. }
  49. -(void)layoutSubviews
  50. {
  51. float left=kLeftX,top=kTopY,hegiht=self.bounds.size.height;
  52. self.placeholdeWidth=CGRectGetWidth(self.frame)-2*left;
  53. CGRect frame=_PlaceholderLabel.frame;
  54. frame.origin.x=left;
  55. frame.origin.y=top;
  56. frame.size.height=hegiht;
  57. frame.size.width=self.placeholdeWidth;
  58. _PlaceholderLabel.frame=frame;
  59. [_PlaceholderLabel sizeToFit];
  60. }
  61. -(void)dealloc{
  62. [_PlaceholderLabel removeFromSuperview];
  63. [[NSNotificationCenter defaultCenter] removeObserver:self];
  64. }
  65. #pragma mark - System Delegate
  66. #pragma mark - custom Delegate
  67. #pragma mark - Event response
  68. -(void)defaultConfig
  69. {
  70. self.placeholder_color = [UIColor lightGrayColor];
  71. self.placeholder_font = [UIFont systemFontOfSize:14];
  72. self.maxTextLength=1000;
  73. self.layoutManager.allowsNonContiguousLayout=NO;
  74. }
  75. -(void)addMaxTextLengthWithMaxLength:(NSInteger)maxLength andEvent:(void (^)(BRPlaceholderTextView *text))limit
  76. {
  77. if (maxLength>0) {
  78. _maxTextLength=maxLength;
  79. }
  80. if (limit) {
  81. _eventBlock=limit;
  82. }
  83. }
  84. -(void)addTextViewBeginEvent:(void (^)(BRPlaceholderTextView *))begin{
  85. _BeginBlock=begin;
  86. }
  87. -(void)addTextViewEndEvent:(void (^)(BRPlaceholderTextView *))End{
  88. _EndBlock=End;
  89. }
  90. -(void)setUpdateHeight:(float)updateHeight{
  91. CGRect frame=self.frame;
  92. frame.size.height=updateHeight;
  93. self.frame=frame;
  94. _updateHeight=updateHeight;
  95. }
  96. //供外部使用的 api
  97. -(void)setPlaceholderFont:(UIFont *)font
  98. {
  99. self.placeholder_font=font;
  100. }
  101. -(void)setPlaceholderColor:(UIColor *)color
  102. {
  103. self.placeholder_color=color;
  104. }
  105. -(void)setPlaceholderOpacity:(float)opacity
  106. {
  107. if (opacity<0) {
  108. opacity=1;
  109. }
  110. self.PlaceholderLabel.layer.opacity=opacity;
  111. }
  112. #pragma mark - Noti Event
  113. -(void)textViewBeginNoti:(NSNotification*)noti{
  114. if (_BeginBlock) {
  115. void(^begin)(BRPlaceholderTextView*text)=_BeginBlock;
  116. begin(self);
  117. }
  118. }
  119. -(void)textViewEndNoti:(NSNotification*)noti{
  120. if (_EndBlock) {
  121. void(^end)(BRPlaceholderTextView*text)=_EndBlock;
  122. end(self);
  123. }
  124. }
  125. -(void)DidChange:(NSNotification*)noti{
  126. if (self.placeholder.length == 0 || [self.placeholder isEqualToString:@""]) {
  127. _PlaceholderLabel.hidden=YES;
  128. }
  129. if (self.text.length > 0) {
  130. _PlaceholderLabel.hidden=YES;
  131. }
  132. else{
  133. _PlaceholderLabel.hidden=NO;
  134. }
  135. NSString *lang = [[self.nextResponder textInputMode] primaryLanguage]; // 键盘输入模式
  136. if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
  137. UITextRange *selectedRange = [self markedTextRange];
  138. //获取高亮部分
  139. UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
  140. // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
  141. if (!position) {
  142. if (self.text.length > self.maxTextLength) {
  143. self.text = [self.text substringToIndex:self.maxTextLength];
  144. }
  145. }
  146. // 有高亮选择的字符串,则暂不对文字进行统计和限制
  147. else{
  148. }
  149. }
  150. // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
  151. else{
  152. if (self.text.length > self.maxTextLength) {
  153. self.text = [ self.text substringToIndex:self.maxTextLength];
  154. }
  155. }
  156. if (_eventBlock && self.text.length > self.maxTextLength) {
  157. void (^limint)(BRPlaceholderTextView*text) =_eventBlock;
  158. limint(self);
  159. }
  160. }
  161. #pragma mark - private method
  162. +(float)boundingRectWithSize:(CGSize)size withLabel:(NSString *)label withFont:(UIFont *)font{
  163. NSDictionary *attribute = @{NSFontAttributeName:font};
  164. // CGSize retSize;
  165. CGSize retSize = [label boundingRectWithSize:size
  166. options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
  167. attributes:attribute
  168. context:nil].size;
  169. return retSize.height;
  170. }
  171. #pragma mark - getters and Setters
  172. -(void)setText:(NSString *)tex{
  173. if (tex.length>0) {
  174. _PlaceholderLabel.hidden=YES;
  175. }
  176. [super setText:tex];
  177. }
  178. -(void)setPlaceholder:(NSString *)placeholder{
  179. if (placeholder.length == 0 || [placeholder isEqualToString:@""]) {
  180. _PlaceholderLabel.hidden=YES;
  181. }
  182. else
  183. {
  184. _PlaceholderLabel.text=placeholder;
  185. _placeholder=placeholder;
  186. // float height= [BRPlaceholderTextView boundingRectWithSize:CGSizeMake(_placeholdeWidth, MAXFLOAT) withLabel:_placeholder withFont:_PlaceholderLabel.font];
  187. // if (height>CGRectGetHeight(_PlaceholderLabel.frame) && height< CGRectGetHeight(self.frame)) {
  188. //
  189. // CGRect frame=_PlaceholderLabel.frame;
  190. // frame.size.height=height;
  191. // _PlaceholderLabel.frame=frame;
  192. //
  193. // }
  194. }
  195. }
  196. -(void)setPlaceholder_font:(UIFont *)placeholder_font
  197. {
  198. _placeholder_font=placeholder_font;
  199. _PlaceholderLabel.font=placeholder_font;
  200. }
  201. -(void)setPlaceholder_color:(UIColor *)placeholder_color
  202. {
  203. _placeholder_color=placeholder_color;
  204. _PlaceholderLabel.textColor=placeholder_color;
  205. }
  206. @end