MOUserLinkCell.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // MOUserLinkCell.m
  3. // TUIChat
  4. //
  5. // Created by SuperC on 2025/10/13.
  6. //
  7. #define UIColorFromHex(HexValue) [UIColor colorWithRed:((float)((HexValue & 0xFF0000) >> 16))/255.0 green:((float)((HexValue & 0xFF00) >> 8))/255.0 blue:((float)(HexValue & 0xFF))/255.0 alpha:1.0]
  8. #import "MOUserLinkCell.h"
  9. @implementation MOUserLinkCell
  10. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  11. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  12. if (self) {
  13. self.bubbleView.hidden = YES;
  14. [self.container addSubview:self.bgView];
  15. [self.container addSubview:self.titleLab];
  16. [self.container addSubview:self.contentLab];
  17. [self.container addSubview:self.lineView];
  18. [self.container addSubview:self.detailTagLab];
  19. [self.container addSubview:self.arrowImgView];
  20. [self.container addSubview:self.actionBtn];
  21. }
  22. return self;
  23. }
  24. - (void)fillWithData:(MOUserLinkCellData *)data{
  25. [super fillWithData:data];
  26. self.dataModel = data;
  27. self.titleLab.text = data.title;
  28. self.contentLab.text = data.content;
  29. // tell constraints they need updating
  30. [self setNeedsUpdateConstraints];
  31. // update constraints now so we can animate the change
  32. [self updateConstraintsIfNeeded];
  33. [self layoutIfNeeded];
  34. }
  35. - (void)awakeFromNib {
  36. [super awakeFromNib];
  37. // Initialization code
  38. }
  39. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  40. [super setSelected:selected animated:animated];
  41. // Configure the view for the selected state
  42. }
  43. + (BOOL)requiresConstraintBasedLayout {
  44. return YES;
  45. }
  46. #pragma mark - Lazy
  47. - (UIView *)bgView{
  48. if(!_bgView){
  49. _bgView = [[UIView alloc] init];
  50. _bgView.backgroundColor = [UIColor whiteColor];
  51. _bgView.layer.cornerRadius = 16.0;
  52. _bgView.layer.masksToBounds = YES;
  53. }
  54. return _bgView;
  55. }
  56. - (UILabel *)titleLab{
  57. if(!_titleLab){
  58. _titleLab = [UILabel new];
  59. _titleLab.numberOfLines = 0;
  60. _titleLab.textColor = UIColorFromHex(0x000000);
  61. _titleLab.textAlignment = NSTextAlignmentLeft;
  62. _titleLab.font = [MOUserLinkCell getTheFontWithSize:16.0 AndFontName:@"Inter-Regular_Medium"];
  63. }
  64. return _titleLab;
  65. }
  66. - (UILabel *)contentLab{
  67. if(!_contentLab){
  68. _contentLab = [UILabel new];
  69. _contentLab.textColor = UIColorFromHex(0x000000);
  70. _contentLab.textAlignment = NSTextAlignmentLeft;
  71. _contentLab.font = [MOUserLinkCell getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"];
  72. _contentLab.numberOfLines = 0;
  73. }
  74. return _contentLab;
  75. }
  76. - (UIView *)lineView{
  77. if(!_lineView){
  78. _lineView = [[UIView alloc] init];
  79. _lineView.backgroundColor = UIColorFromHex(0xF3F4FA);
  80. }
  81. return _lineView;
  82. }
  83. - (UILabel *)detailTagLab{
  84. if(!_detailTagLab){
  85. _detailTagLab = [UILabel new];
  86. _detailTagLab.textColor = UIColorFromHex(0x4363FF);
  87. _detailTagLab.textAlignment = NSTextAlignmentLeft;
  88. _detailTagLab.font = [MOUserLinkCell getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"];
  89. _detailTagLab.numberOfLines = 0;
  90. _detailTagLab.text = @"View details";
  91. }
  92. return _detailTagLab;
  93. }
  94. - (UIImageView *)arrowImgView{
  95. if(!_arrowImgView){
  96. _arrowImgView = [[UIImageView alloc] init];
  97. [_arrowImgView setImage:[UIImage imageNamed:@"icon_im_arrow" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]];
  98. }
  99. return _arrowImgView;
  100. }
  101. - (UIButton *)actionBtn{
  102. if(!_actionBtn){
  103. _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  104. [_actionBtn setTitle:@"" forState:UIControlStateNormal];
  105. [_actionBtn addTarget:self action:@selector(actionBtnClick) forControlEvents:UIControlEventTouchUpInside];
  106. }
  107. return _actionBtn;
  108. }
  109. - (void)actionBtnClick{
  110. if (self.dataModel.link) {
  111. [[NSNotificationCenter defaultCenter] postNotificationName:@"MOChatJumpLink" object:self.dataModel.link];
  112. }
  113. }
  114. // this is Apple's recommended place for adding/updating constraints
  115. - (void)updateConstraints{
  116. [super updateConstraints];
  117. CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 64.0 - 12.0;
  118. CGFloat contentWidth = viewWidth - 12.0 * 2;
  119. CGRect titleRect = [self.titleLab.text boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  120. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  121. attributes:@{NSFontAttributeName : [MOUserLinkCell getTheFontWithSize:16.0 AndFontName:@"Inter-Regular_Medium"]}
  122. context:nil];
  123. CGRect contentRect = [self.contentLab.text boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  124. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  125. attributes:@{NSFontAttributeName : [MOUserLinkCell getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"]}
  126. context:nil];
  127. [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  128. make.edges.equalTo(self.container);
  129. }];
  130. [self.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
  131. make.top.mas_equalTo(12);
  132. make.leading.mas_equalTo(12);
  133. make.width.mas_equalTo(245);
  134. make.height.mas_equalTo(titleRect.size.height);
  135. }];
  136. [self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
  137. make.top.mas_equalTo(self.titleLab.mas_bottom).mas_offset(8);
  138. make.leading.mas_equalTo(12);
  139. make.width.mas_equalTo(245);
  140. make.height.mas_equalTo(contentRect.size.height);
  141. }];
  142. [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
  143. make.leading.mas_equalTo(12);
  144. make.trailing.mas_equalTo(0);
  145. make.height.mas_equalTo(0.5);
  146. make.bottom.mas_equalTo(-40);
  147. }];
  148. [self.detailTagLab mas_remakeConstraints:^(MASConstraintMaker *make) {
  149. make.leading.mas_equalTo(12);
  150. make.height.mas_equalTo(17);
  151. make.bottom.mas_equalTo(-12);
  152. }];
  153. [self.arrowImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  154. make.centerY.equalTo(self.detailTagLab);
  155. make.trailing.mas_equalTo(-12);
  156. make.width.height.mas_equalTo(14);
  157. }];
  158. [self.actionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  159. make.edges.equalTo(self.container);
  160. }];
  161. }
  162. - (void)layoutSubviews {
  163. [super layoutSubviews];
  164. }
  165. #pragma mark - TUIMessageCellProtocol
  166. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  167. MOUserLinkCellData *evaluationCellData = (MOUserLinkCellData *)data;
  168. CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 64.0 - 12.0;
  169. CGFloat contentWidth = viewWidth - 12.0 * 2;
  170. //标题以上固定距离
  171. CGFloat titleTopSpacing = 0.0;
  172. //标题高度
  173. CGFloat titleHeight = 0;
  174. if (evaluationCellData.title.length > 0) {
  175. UIFont *titleFont = [self getTheFontWithSize:16.0 AndFontName:@"Inter-Regular_Medium"];
  176. titleHeight = [evaluationCellData.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  177. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont}
  178. context:nil].size.height + 2;//2是补偿高度
  179. }
  180. //内容高度
  181. CGFloat contentHeight = 0;
  182. if(evaluationCellData.content.length > 0){
  183. UIFont *contentFont = [self getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"];
  184. contentHeight = [evaluationCellData.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  185. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  186. attributes:@{NSFontAttributeName : contentFont}
  187. context:nil].size.height;
  188. }
  189. //30是bgView上下距离
  190. CGFloat bgViewBottomSpacing = 0.0;
  191. CGFloat totalHeight = titleTopSpacing + 12.0 + titleHeight + 8.0 + contentHeight + 10.0 + 40.0 + bgViewBottomSpacing;
  192. CGSize size = CGSizeMake(viewWidth, ceilf(totalHeight));
  193. return size;
  194. }
  195. #pragma mark - 类方法
  196. + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
  197. UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
  198. if(!customFont){
  199. customFont = [MOUserLinkCell MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
  200. }
  201. return customFont;
  202. }
  203. + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
  204. bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
  205. UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
  206. UIFontDescriptorSymbolicTraits symbolicTraits = 0;
  207. if (italic) {
  208. symbolicTraits |= UIFontDescriptorTraitItalic;
  209. }
  210. if (bold) {
  211. symbolicTraits |= UIFontDescriptorTraitBold;
  212. }
  213. UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
  214. return specialFont;
  215. }
  216. @end