YOUPAILCOneImageContentView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // YOUPAILCOneImageContentView.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/22.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCOneImageContentView.h"
  9. #import "YOUPAILCOneImageAttachment.h"
  10. @interface YOUPAILCOneImageContentView()
  11. @property(nonatomic,strong)UIView* backView;
  12. @property(nonatomic,strong)UIImageView* imgView;
  13. @property(nonatomic,strong)UILabel* topTitle;
  14. @property(nonatomic,strong)UILabel* txtTitle;
  15. @property(nonatomic,assign)CGFloat currentY;
  16. @end
  17. @implementation YOUPAILCOneImageContentView
  18. - (instancetype)initSessionMessageContentView{
  19. self = [super initSessionMessageContentView];
  20. if (self) {
  21. self.opaque = YES;
  22. UIView* testView = [[UIView alloc] initWithFrame:CGRectZero];
  23. testView.backgroundColor= [UIColor whiteColor];
  24. testView.layer.cornerRadius = 6.0;
  25. UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandel:)];
  26. [testView addGestureRecognizer:tap];
  27. self.backView = testView;
  28. self.bubbleImageView.hidden = YES;
  29. [self addSubview:testView];
  30. }
  31. return self;
  32. }
  33. - (void)refresh:(NIMMessageModel *)data{
  34. [super refresh:data];
  35. NIMCustomObject *customObject = (NIMCustomObject*)data.message.messageObject;
  36. id attachment = customObject.attachment;
  37. if ([attachment isKindOfClass:[YOUPAILCOneImageAttachment class]]) {
  38. for (UIView* subView in self.backView.subviews) {
  39. [subView removeFromSuperview];
  40. }
  41. self.currentY = (KScreenWidth-30)*9/16;
  42. YOUPAILCOneImageAttachment* attachment = (YOUPAILCOneImageAttachment*)customObject.attachment;
  43. UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-30, (KScreenWidth-30)*9/16)];
  44. imgView.backgroundColor = LCGray;
  45. self.imgView = imgView;
  46. if (attachment.act_type==1) {
  47. [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight View:imgView size:CGSizeMake(6, 6)];
  48. if (attachment.act_string.length>0) {
  49. UILabel* logoLabel = [[UILabel alloc]init];
  50. logoLabel.text = attachment.act_string;
  51. logoLabel.font = LCFont14;
  52. logoLabel.textColor = [UIColor whiteColor];
  53. logoLabel.backgroundColor = HexColorFromRGBA(0x000000, 0.5);
  54. logoLabel.layer.cornerRadius = 10;
  55. logoLabel.textAlignment = NSTextAlignmentCenter;
  56. logoLabel.layer.masksToBounds = YES;
  57. CGFloat logoWidth =[attachment.act_string widthWithFont:LCFont14 constrainedToHeight:20];
  58. logoLabel.frame = CGRectMake(KScreenWidth-30-logoWidth-20-20,20,logoWidth+20, 20);
  59. [imgView addSubview:logoLabel];
  60. }
  61. }else{
  62. [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:imgView size:CGSizeMake(6, 6)];
  63. }
  64. imgView.layer.masksToBounds = YES;
  65. [self.backView addSubview:imgView];
  66. [self.imgView sd_setImageWithURL:[NSURL URLWithString:attachment.image] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  67. if (image) {
  68. self.imgView.image = image;
  69. }
  70. }];
  71. if (attachment.act_type==2) {
  72. if (![attachment.title isEqualToString:@""]) {
  73. UILabel* txtOneLabel = [[UILabel alloc]init];
  74. txtOneLabel.numberOfLines = 0;
  75. txtOneLabel.text = attachment.title;
  76. txtOneLabel.font = [UIFont boldSystemFontOfSize:16];
  77. txtOneLabel.textColor = LCTextBlack;
  78. CGFloat txtOneHeight =[attachment.title heightWithFont:[UIFont boldSystemFontOfSize:16] padding:3.0 constrainedToWidth:KScreenWidth-70];
  79. txtOneLabel.frame = CGRectMake(20, self.currentY+20, KScreenWidth-70, txtOneHeight+2);
  80. [UILabel changeLineSpaceForLabel:txtOneLabel WithSpace:3.0];
  81. [self.backView addSubview:txtOneLabel];
  82. self.currentY +=txtOneHeight+22;
  83. }
  84. if (![attachment.txt isEqualToString:@""]) {
  85. UILabel* txtTwoLabel = [[UILabel alloc]init];
  86. txtTwoLabel.text = attachment.txt;
  87. txtTwoLabel.numberOfLines = 0;
  88. txtTwoLabel.font = [UIFont systemFontOfSize:15];
  89. txtTwoLabel.textColor = LCTextNormal;
  90. CGFloat txtTwoHeight =[attachment.txt heightWithFont:[UIFont systemFontOfSize:15] padding:3.0 constrainedToWidth:KScreenWidth-70];
  91. txtTwoLabel.frame = CGRectMake(20, self.currentY+10, KScreenWidth-70, txtTwoHeight+2);
  92. [UILabel changeLineSpaceForLabel:txtTwoLabel WithSpace:3.0];
  93. [self.backView addSubview:txtTwoLabel];
  94. self.currentY +=txtTwoHeight+32;
  95. }
  96. }
  97. }
  98. }
  99. - (void)tapHandel:(UITapGestureRecognizer*)tap{
  100. if (tap.state == UIGestureRecognizerStateEnded) {
  101. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  102. event.eventName = NIMKitEventNameTapContent;
  103. event.messageModel = self.model;
  104. [self.delegate onCatchEvent:event];
  105. }
  106. }
  107. - (void)onTouchUpInside:(id)sender
  108. {
  109. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  110. event.eventName = NIMKitEventNameTapContent;
  111. event.messageModel = self.model;
  112. [self.delegate onCatchEvent:event];
  113. }
  114. - (void)layoutSubviews{
  115. [super layoutSubviews];
  116. UIEdgeInsets contentInsets = self.model.contentViewInsets;
  117. // CGFloat tableViewWidth = self.superview.nim_width;
  118. CGSize contentsize = [self.model contentSize:KScreenWidth];
  119. CGRect imageViewFrame = CGRectMake(contentInsets.left, contentInsets.top, contentsize.width, contentsize.height);
  120. self.backView.frame = imageViewFrame;
  121. }
  122. @end