JCHATMessageTextView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // JCHATMessageTextView.m
  3. // JPush IM
  4. //
  5. // Created by Apple on 15/1/14.
  6. // Copyright (c) 2015年 Apple. All rights reserved.
  7. //
  8. #import "JCHATMessageTextView.h"
  9. #import "JChatConstants.h"
  10. #import "JCHATAlertToSendImage.h"
  11. @implementation JCHATMessageTextView
  12. #pragma mark - Setters
  13. - (void)setPlaceHolder:(NSString *)placeHolder {
  14. if([placeHolder isEqualToString:_placeHolder]) {
  15. return;
  16. }
  17. NSUInteger maxChars = [JCHATMessageTextView maxCharactersPerLine];
  18. if([placeHolder length] > maxChars) {
  19. placeHolder = [placeHolder substringToIndex:maxChars - 8];
  20. placeHolder = [[placeHolder stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAppendingFormat:@"..."];
  21. }
  22. _placeHolder = placeHolder;
  23. [self setNeedsDisplay];
  24. }
  25. - (UIResponder *)nextResponder {
  26. if(_overrideNextResponder == nil){
  27. return [super nextResponder];
  28. } else {
  29. return _overrideNextResponder;
  30. }
  31. }
  32. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
  33. if (_overrideNextResponder != nil) {
  34. return NO;
  35. }
  36. if(action ==@selector(copy:) ||
  37. action ==@selector(selectAll:)||
  38. action ==@selector(cut:)||
  39. action ==@selector(select:)||
  40. action ==@selector(paste:)) {
  41. return[super canPerformAction:action withSender:sender];
  42. }
  43. return NO;
  44. }
  45. /*
  46. //"反馈"关心的功能
  47. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
  48. return (action == @selector(paste:));
  49. }
  50. - (void)paste:(id)sender {
  51. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  52. NSTextAttachment *textAttachment = [NSTextAttachment new];
  53. if (pasteboard.string != nil) {
  54. [super paste:sender];
  55. return;
  56. }
  57. if (pasteboard.image != nil) {
  58. textAttachment.image = pasteboard.image;
  59. NSAttributedString *attString = [NSAttributedString attributedStringWithAttachment:textAttachment];
  60. [[JCHATAlertToSendImage shareInstance] showInViewWith:pasteboard.image];
  61. }
  62. }
  63. */
  64. - (void)setPlaceHolderTextColor:(UIColor *)placeHolderTextColor {
  65. if([placeHolderTextColor isEqual:_placeHolderTextColor]) {
  66. return;
  67. }
  68. _placeHolderTextColor = placeHolderTextColor;
  69. [self setNeedsDisplay];
  70. }
  71. #pragma mark - Message text view
  72. - (NSUInteger)numberOfLinesOfText {
  73. return [JCHATMessageTextView numberOfLinesForMessage:self.text];
  74. }
  75. + (NSUInteger)maxCharactersPerLine {
  76. return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 33 : 109;
  77. }
  78. + (NSUInteger)numberOfLinesForMessage:(NSString *)text {
  79. return (text.length / [JCHATMessageTextView maxCharactersPerLine]) + 1;
  80. }
  81. #pragma mark - Text view overrides
  82. - (void)setText:(NSString *)text {
  83. [super setText:text];
  84. [self setNeedsDisplay];
  85. }
  86. - (void)setAttributedText:(NSAttributedString *)attributedText {
  87. [super setAttributedText:attributedText];
  88. [self setNeedsDisplay];
  89. }
  90. - (void)setContentInset:(UIEdgeInsets)contentInset {
  91. [super setContentInset:contentInset];
  92. [self setNeedsDisplay];
  93. }
  94. - (void)setFont:(UIFont *)font {
  95. JCHATMAINTHREAD(^{
  96. [super setFont:font];
  97. [self setNeedsDisplay];
  98. });
  99. }
  100. - (void)setTextAlignment:(NSTextAlignment)textAlignment {
  101. [super setTextAlignment:textAlignment];
  102. [self setNeedsDisplay];
  103. }
  104. #pragma mark - Notifications
  105. - (void)didReceiveTextDidChangeNotification:(NSNotification *)notification {
  106. [self setNeedsDisplay];
  107. }
  108. #pragma mark - Life cycle
  109. - (void)setup {
  110. [[NSNotificationCenter defaultCenter] addObserver:self
  111. selector:@selector(didReceiveTextDidChangeNotification:)
  112. name:UITextViewTextDidChangeNotification
  113. object:self];
  114. _placeHolderTextColor = [UIColor lightGrayColor];
  115. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  116. self.scrollIndicatorInsets = UIEdgeInsetsMake(4.0f, 0.0f, 4.0f, 8.0f);
  117. self.contentInset = UIEdgeInsetsZero;
  118. self.scrollEnabled = YES;
  119. self.scrollsToTop = NO;
  120. self.userInteractionEnabled = YES;
  121. self.font = [UIFont systemFontOfSize:16.0f];
  122. self.textColor = [UIColor blackColor];
  123. self.backgroundColor = [UIColor whiteColor];
  124. self.keyboardAppearance = UIKeyboardAppearanceDefault;
  125. self.keyboardType = UIKeyboardTypeDefault;
  126. self.returnKeyType = UIReturnKeyDefault;
  127. self.textAlignment = NSTextAlignmentLeft;
  128. self.layer.cornerRadius = 6;
  129. [self.layer setMasksToBounds:YES];
  130. }
  131. -(void)awakeFromNib {
  132. [super awakeFromNib];
  133. [self setup];
  134. }
  135. - (id)initWithFrame:(CGRect)frame {
  136. self = [super initWithFrame:frame];
  137. if (self) {
  138. // Initialization code
  139. [self setup];
  140. }
  141. return self;
  142. }
  143. - (void)dealloc {
  144. _placeHolder = nil;
  145. _placeHolderTextColor = nil;
  146. [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
  147. }
  148. #pragma mark - Drawing
  149. - (void)drawRect:(CGRect)rect {
  150. [super drawRect:rect];
  151. if([self.text isEqual:@""]) {
  152. CGRect placeHolderRect = CGRectMake(8.0f,
  153. 7.0f,
  154. rect.size.width,
  155. rect.size.height);
  156. [self.placeHolderTextColor set];
  157. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_0) {
  158. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  159. paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
  160. paragraphStyle.alignment = self.textAlignment;
  161. [self.placeHolder drawInRect:placeHolderRect
  162. withAttributes:@{ NSFontAttributeName : self.font,
  163. NSForegroundColorAttributeName : self.placeHolderTextColor,
  164. NSParagraphStyleAttributeName : paragraphStyle }];
  165. }
  166. else {
  167. NSMutableParagraphStyle *paragraphStyle= [[NSMutableParagraphStyle alloc] init];
  168. paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
  169. paragraphStyle.alignment = self.textAlignment;
  170. [self.placeHolder drawInRect:placeHolderRect withAttributes:@{NSFontAttributeName:self.font,NSParagraphStyleAttributeName:paragraphStyle}];
  171. }
  172. }
  173. }
  174. /*
  175. // Only override drawRect: if you perform custom drawing.
  176. // An empty implementation adversely affects performance during animation.
  177. - (void)drawRect:(CGRect)rect {
  178. // Drawing code
  179. }
  180. */
  181. @end