QMChatRoomRichTextCell.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // QMChatRoomRichTextCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by lishuijiao on 2018/3/8.
  6. // Copyright © 2018年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomRichTextCell.h"
  9. #import "QMChatRoomShowRichTextController.h"
  10. #import "QMAlert.h"
  11. /**
  12. 富文本消息
  13. */
  14. @interface QMChatRoomRichTextCell() <MLEmojiLabelDelegate>
  15. @end
  16. @implementation QMChatRoomRichTextCell
  17. {
  18. NSString *_messageId;
  19. UIView *_richView;
  20. UILabel *_titleLabel;
  21. UILabel *_descriptionLabel;
  22. UILabel *_price;
  23. UIImageView *_imageView;
  24. }
  25. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  26. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  27. if (self) {
  28. [self createUI];
  29. }
  30. return self;
  31. }
  32. - (void)createUI {
  33. _richView = [[UIView alloc] init];
  34. _richView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 160, 110);
  35. _richView.clipsToBounds = YES;
  36. [self.contentView addSubview:_richView];
  37. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
  38. [_richView addGestureRecognizer:tap];
  39. _titleLabel = [[UILabel alloc] init];
  40. _titleLabel.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 180, 30);
  41. _titleLabel.font = [UIFont systemFontOfSize:15.0f];
  42. _titleLabel.numberOfLines = 2;
  43. [_richView addSubview:_titleLabel];
  44. _descriptionLabel = [[UILabel alloc] init];
  45. _descriptionLabel.frame = CGRectMake(10, _titleLabel.frame.size.height + 15, [UIScreen mainScreen].bounds.size.width - 250, 50);
  46. _descriptionLabel.numberOfLines = 0;
  47. _descriptionLabel.font = [UIFont systemFontOfSize:12.0f];
  48. _descriptionLabel.textColor = [UIColor colorWithRed:89/255.0 green:89/255.0 blue:89/255.0 alpha:1];
  49. [_richView addSubview:_descriptionLabel];
  50. _price = [[UILabel alloc] init];
  51. _price.font = [UIFont systemFontOfSize:12.0f];
  52. _price.textColor = [UIColor colorWithRed:255/255.0 green:107/255.0 blue:107/255.0 alpha:1];
  53. [_richView addSubview:_price];
  54. _imageView = [[UIImageView alloc] init];
  55. _imageView.frame = CGRectMake( _descriptionLabel.frame.size.width+20, _titleLabel.frame.size.height + 15, 60, 50);
  56. [_richView addSubview:_imageView];
  57. }
  58. - (void)setData:(CustomMessage *)message avater:(NSString *)avater {
  59. self.message = message;
  60. _messageId = message._id;
  61. [super setData:message avater:avater];
  62. if ([message.fromType isEqualToString:@"1"]) {
  63. _titleLabel.textColor = [UIColor blackColor];
  64. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, [UIScreen mainScreen].bounds.size.width - 160, 110);
  65. _richView.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+5, [UIScreen mainScreen].bounds.size.width - 160, 120);
  66. _titleLabel.text = message.richTextTitle;
  67. if (_titleLabel.text != nil) {
  68. CGFloat height = [self calculateRowHeight:_titleLabel.text fontSize:15];
  69. if (height > 20) {
  70. _titleLabel.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 180, 40);
  71. _imageView.frame = CGRectMake( [UIScreen mainScreen].bounds.size.width - 230, _titleLabel.frame.size.height + 15, 60, 50);
  72. _descriptionLabel.frame = CGRectMake(10, _titleLabel.frame.size.height + 15, [UIScreen mainScreen].bounds.size.width - 250, 50);
  73. }
  74. NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:_titleLabel.text];
  75. NSRange range = NSMakeRange(0, content.length);
  76. [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
  77. _titleLabel.attributedText = content;
  78. }
  79. _descriptionLabel.text = message.richTextDescription;
  80. if ([message.richTextPicUrl isEqual: @""]) {
  81. _descriptionLabel.frame = CGRectMake(10, _titleLabel.frame.size.height + 15, [UIScreen mainScreen].bounds.size.width - 180, 50);
  82. _imageView.hidden = YES;
  83. }else{
  84. _descriptionLabel.frame = CGRectMake(10, _titleLabel.frame.size.height + 15, [UIScreen mainScreen].bounds.size.width - 250, 50);
  85. _imageView.hidden = NO;
  86. [_imageView sd_setImageWithURL:[NSURL URLWithString:message.richTextPicUrl] placeholderImage:[UIImage imageNamed:@""]];
  87. }
  88. [_descriptionLabel sizeToFit];
  89. }else{
  90. self.sendStatus.frame = CGRectMake(CGRectGetMinX(self.chatBackgroudImage.frame)-25, CGRectGetMinY(self.chatBackgroudImage.frame)+15, 20, 20);
  91. _richView.frame = CGRectMake(60, CGRectGetMaxY(self.timeLabel.frame)+5, [UIScreen mainScreen].bounds.size.width - 120, 80);
  92. self.chatBackgroudImage.frame = CGRectMake(60, CGRectGetMaxY(self.timeLabel.frame)+5, [UIScreen mainScreen].bounds.size.width - 120, 80);
  93. _titleLabel.text = message.cardHeader;
  94. if (message.cardSubhead != nil) {
  95. _descriptionLabel.text = message.cardSubhead;
  96. }
  97. if (message.cardPrice != nil) {
  98. _price.text = message.cardPrice;
  99. }
  100. if ([message.cardImage isEqual: @""]) {
  101. _titleLabel.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 140, 25);
  102. _descriptionLabel.frame = CGRectMake(10, CGRectGetMaxY(_titleLabel.frame) + 5, [UIScreen mainScreen].bounds.size.width - 140, 15);
  103. _price.frame = CGRectMake(10, CGRectGetMaxY(_descriptionLabel.frame) + 5, [UIScreen mainScreen].bounds.size.width - 140, 15);
  104. }else{
  105. _imageView.frame = CGRectMake(10, 10, 60, 60);
  106. _titleLabel.frame = CGRectMake(80, 10, [UIScreen mainScreen].bounds.size.width - 200, 25);
  107. _descriptionLabel.frame = CGRectMake(80, CGRectGetMaxY(_titleLabel.frame) + 5, [UIScreen mainScreen].bounds.size.width - 220, 15);
  108. _price.frame = CGRectMake(80, CGRectGetMaxY(_descriptionLabel.frame) + 5, [UIScreen mainScreen].bounds.size.width - 220, 15);
  109. _imageView.hidden = NO;
  110. [_imageView sd_setImageWithURL:[NSURL URLWithString:message.cardImage] placeholderImage:[UIImage imageNamed:@""]];
  111. }
  112. UIImage *image = [UIImage imageNamed:@"SenderCardNodeBkg"];
  113. self.chatBackgroudImage.image = image;
  114. self.chatBackgroudImage.image = [self.chatBackgroudImage.image stretchableImageWithLeftCapWidth:20 topCapHeight:20];
  115. }
  116. }
  117. - (void)tapAction:(UITapGestureRecognizer *)gestureRecognizer {
  118. NSLog(@"点击富文本");
  119. QMChatRoomShowRichTextController * showWebVC = [[QMChatRoomShowRichTextController alloc] init];
  120. if ([self.message.fromType isEqualToString:@"1"]) {
  121. showWebVC.urlStr = self.message.richTextUrl;
  122. }else{
  123. showWebVC.urlStr = self.message.cardUrl;
  124. }
  125. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showWebVC animated:true completion:nil];
  126. }
  127. - (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize {
  128. NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};//指定字号
  129. CGRect rect = [string boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 180, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
  130. NSStringDrawingUsesFontLeading attributes:dic context:nil];
  131. return rect.size.height;
  132. }
  133. - (void)awakeFromNib {
  134. [super awakeFromNib];
  135. // Initialization code
  136. }
  137. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  138. [super setSelected:selected animated:animated];
  139. // Configure the view for the selected state
  140. }
  141. @end