QMChatRoomTextCell.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // QMChatRoomTextCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/3/10.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomTextCell.h"
  9. #import "QMChatRoomRobotReplyView.h"
  10. #import <QMLineSDK/QMLineSDK.h>
  11. /**
  12. 文本消息
  13. */
  14. @interface QMChatRoomTextCell() <MLEmojiLabelDelegate>
  15. @end
  16. @implementation QMChatRoomTextCell
  17. {
  18. MLEmojiLabel *_textLabel;
  19. NSString *_messageId;
  20. }
  21. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  22. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  23. if (self) {
  24. [self createUI];
  25. }
  26. return self;
  27. }
  28. - (void)createUI {
  29. _textLabel = [MLEmojiLabel new];
  30. _textLabel.numberOfLines = 0;
  31. _textLabel.font = [UIFont systemFontOfSize:14.0f];
  32. _textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  33. _textLabel.delegate = self;
  34. _textLabel.disableEmoji = NO;
  35. _textLabel.disableThreeCommon = NO;
  36. _textLabel.isNeedAtAndPoundSign = YES;
  37. _textLabel.customEmojiRegex = @"\\:[^\\:]+\\:";
  38. _textLabel.customEmojiPlistName = @"expressionImage.plist";
  39. _textLabel.customEmojiBundleName = @"QMEmoticon.bundle";
  40. [self.chatBackgroudImage addSubview:_textLabel];
  41. UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTapGesture:)];
  42. [_textLabel addGestureRecognizer:longPressGesture];
  43. }
  44. - (void)setData:(CustomMessage *)message avater:(NSString *)avater {
  45. self.message = message;
  46. _messageId = message._id;
  47. [super setData:message avater:avater];
  48. if ([message.fromType isEqualToString:@"0"]) {
  49. _textLabel.textColor = [UIColor whiteColor];
  50. _textLabel.text = message.message;
  51. CGSize size = [_textLabel preferredSizeWithMaxWidth: [UIScreen mainScreen].bounds.size.width - 160];
  52. _textLabel.frame = CGRectMake(15, 10, size.width, size.height+5);
  53. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMinX(self.iconImage.frame)-5-_textLabel.frame.size.width-30, CGRectGetMaxY(self.timeLabel.frame)+10, _textLabel.frame.size.width+30, _textLabel.frame.size.height+20);
  54. self.sendStatus.frame = CGRectMake(CGRectGetMinX(self.chatBackgroudImage.frame)-25, CGRectGetMinY(self.chatBackgroudImage.frame)+10, 20, 20);
  55. }else {
  56. _textLabel.textColor = [UIColor blackColor];
  57. _textLabel.text = message.message;
  58. CGSize size = [_textLabel preferredSizeWithMaxWidth: [UIScreen mainScreen].bounds.size.width - 160];
  59. _textLabel.frame = CGRectMake(15, 10, size.width, size.height+5);
  60. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, _textLabel.frame.size.width+30, _textLabel.frame.size.height+20);
  61. }
  62. }
  63. - (void)longPressTapGesture:(UILongPressGestureRecognizer *)sender {
  64. if (sender.state == UIGestureRecognizerStateBegan) {
  65. [self becomeFirstResponder];
  66. UIMenuController *menu = [UIMenuController sharedMenuController];
  67. UIMenuItem *copyMenu = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"button.copy", nil) action:@selector(copyMenu:)];
  68. UIMenuItem *removeMenu = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"button.delete", nil) action:@selector(removeMenu:)];
  69. [menu setMenuItems:[NSArray arrayWithObjects:copyMenu,removeMenu, nil]];
  70. [menu setTargetRect:self.chatBackgroudImage.frame inView:self];
  71. [menu setMenuVisible:true animated:true];
  72. UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
  73. if ([window isKeyWindow] == NO) {
  74. [window becomeKeyWindow];
  75. [window makeKeyAndVisible];
  76. }
  77. }
  78. }
  79. - (void)helpBtnAction: (UIButton *)sender {
  80. NSLog(@"帮助点击");
  81. self.didBtnAction(YES);
  82. }
  83. - (void)noHelpBtnAction: (UIButton *)sender {
  84. NSLog(@"没有帮助点击");
  85. self.didBtnAction(NO);
  86. }
  87. - (BOOL)canBecomeFirstResponder {
  88. return YES;
  89. }
  90. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  91. if (action == @selector(copyMenu:) || action == @selector(removeMenu:)) {
  92. return YES;
  93. }else {
  94. return NO;
  95. }
  96. }
  97. - (void)copyMenu:(id)sender {
  98. // 复制文本消息
  99. UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
  100. pasteBoard.string = _textLabel.text;
  101. }
  102. - (void)removeMenu:(id)sender {
  103. // 删除文本消息
  104. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"title.prompt", nil) message:NSLocalizedString(@"title.statement", nil) preferredStyle:UIAlertControllerStyleAlert];
  105. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"button.cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  106. }];
  107. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"button.sure", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  108. [QMConnect removeDataFromDataBase:_messageId];
  109. [[NSNotificationCenter defaultCenter] postNotificationName:CHATMSG_RELOAD object:nil];
  110. }];
  111. [alertController addAction:cancelAction];
  112. [alertController addAction:sureAction];
  113. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  114. }
  115. - (void)mlEmojiLabel:(MLEmojiLabel *)emojiLabel didSelectLink:(NSString *)link withType:(MLEmojiLabelLinkType)type {
  116. NSLog(@"%@, %lu", link, (unsigned long)type);
  117. if (link) {
  118. self.tapNetAddress(link);
  119. }
  120. }
  121. - (void)awakeFromNib {
  122. // Initialization code
  123. [super awakeFromNib];
  124. }
  125. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  126. [super setSelected:selected animated:animated];
  127. // Configure the view for the selected state
  128. }
  129. @end