WFCULinkCell.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // WFCUCardCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCULinkCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #import "UILabel+YBAttributeTextTapAction.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import "WFCUImage.h"
  14. @interface WFCULinkCell ()
  15. @property (nonatomic, strong)UIImageView *thumbnailImageView;
  16. @property (nonatomic, strong)UILabel *TitleLabel;
  17. @property (nonatomic, strong)UILabel *contentLabel;
  18. @end
  19. @implementation WFCULinkCell
  20. + (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  21. WFCCLinkMessageContent *content = (WFCCLinkMessageContent *)msgModel.message.content;
  22. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.title font:[UIFont systemFontOfSize:18] constrainedSize:CGSizeMake(width, 50)];
  23. CGFloat contentWidth = width - 56;
  24. NSString *contentTxt = content.url;
  25. if (content.contentDigest.length) {
  26. contentTxt = content.contentDigest;
  27. }
  28. CGSize contentSize = [WFCUUtilities getTextDrawingSize:contentTxt font:[UIFont systemFontOfSize:14] constrainedSize:CGSizeMake(contentWidth, 68)];
  29. CGFloat height = titleSize.height + 4 + MAX(contentSize.height, 56);
  30. return CGSizeMake(width, height);
  31. }
  32. - (void)setModel:(WFCUMessageModel *)model {
  33. [super setModel:model];
  34. WFCCLinkMessageContent *content = (WFCCLinkMessageContent *)model.message.content;
  35. CGFloat width = self.contentArea.bounds.size.width;
  36. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.title font:[UIFont systemFontOfSize:18] constrainedSize:CGSizeMake(width, 50)];
  37. CGFloat contentWidth = width - 56;
  38. NSString *contentTxt = content.url;
  39. if (content.contentDigest.length) {
  40. contentTxt = content.contentDigest;
  41. }
  42. CGSize contentSize = [WFCUUtilities getTextDrawingSize:contentTxt font:[UIFont systemFontOfSize:14] constrainedSize:CGSizeMake(contentWidth, 68)];
  43. self.TitleLabel.text = content.title;
  44. self.TitleLabel.frame = CGRectMake(0, 0, width, titleSize.height);
  45. self.contentLabel.text = contentTxt;
  46. self.contentLabel.frame = CGRectMake(0, titleSize.height+4, contentWidth, contentSize.height);
  47. [self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:content.thumbnailUrl] placeholderImage:[WFCUImage imageNamed:@"default_link"]];
  48. self.thumbnailImageView.frame = CGRectMake(contentWidth+4, titleSize.height+4, 48, 48);
  49. }
  50. - (UIImageView *)thumbnailImageView {
  51. if (!_thumbnailImageView) {
  52. _thumbnailImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 48, 48)];
  53. [self.contentArea addSubview:_thumbnailImageView];
  54. }
  55. return _thumbnailImageView;
  56. }
  57. - (UILabel *)TitleLabel {
  58. if (!_TitleLabel) {
  59. CGRect bounds = self.contentArea.bounds;
  60. _TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 10, bounds.size.width - 72 - 8, 24)];
  61. _TitleLabel.font = [UIFont systemFontOfSize:18];
  62. _TitleLabel.numberOfLines = 0;
  63. [self.contentArea addSubview:_TitleLabel];
  64. }
  65. return _TitleLabel;
  66. }
  67. - (UILabel *)contentLabel {
  68. if (!_contentLabel) {
  69. CGRect bounds = self.contentArea.bounds;
  70. _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 40, bounds.size.width - 72 - 8, 18)];
  71. _contentLabel.font = [UIFont systemFontOfSize:14];
  72. _contentLabel.textColor = [UIColor grayColor];
  73. _contentLabel.numberOfLines = 0;
  74. [self.contentArea addSubview:_contentLabel];
  75. }
  76. return _contentLabel;
  77. }
  78. @end