MOSystemNormalCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // MOSystemNormalCell.m
  3. // TUIChat
  4. //
  5. // Created by SuperC on 2025/5/16.
  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 "MOSystemNormalCell.h"
  9. @implementation MOSystemNormalCell
  10. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  11. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  12. if(self){
  13. self.backgroundColor = [UIColor clearColor];
  14. self.contentView.backgroundColor = [UIColor clearColor];
  15. //Cell 去除选中效果
  16. self.selectionStyle = UITableViewCellSelectionStyleNone;
  17. [self.contentView addSubview:self.bgView];
  18. [self.bgView addSubview:self.iconImgView];
  19. [self.bgView addSubview:self.groupLabel];
  20. [self.bgView addSubview:self.titleLab];
  21. [self.bgView addSubview:self.timeLab];
  22. [self.bgView addSubview:self.pictureView];
  23. [self.bgView addSubview:self.contentLab];
  24. [self.bgView addSubview:self.detailsView];
  25. [self setupView];
  26. }
  27. return self;
  28. }
  29. - (void)setupView{
  30. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  31. make.leading.equalTo(self.contentView).offset(15.0);
  32. make.trailing.equalTo(self.contentView).offset(-15.0);
  33. make.top.equalTo(self.contentView).offset(7.5);
  34. make.bottom.equalTo(self.contentView).offset(-7.5);
  35. }];
  36. [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.leading.equalTo(self.bgView).offset(10.0);
  38. make.top.equalTo(self.bgView).offset(8.0);
  39. make.size.mas_equalTo(CGSizeMake(24, 24));
  40. }];
  41. [self.groupLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.leading.mas_equalTo(40);
  43. make.top.mas_equalTo(13);
  44. make.width.lessThanOrEqualTo(@180);
  45. }];
  46. [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.top.mas_equalTo(40);
  48. make.leading.mas_equalTo(10);
  49. make.trailing.mas_equalTo(-10);
  50. make.height.mas_equalTo(75);
  51. }];
  52. [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.trailing.equalTo(self.bgView).offset(-10.0);
  54. make.top.mas_equalTo(13);
  55. make.width.lessThanOrEqualTo(@180);
  56. }];
  57. [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.top.mas_equalTo(40);
  59. make.leading.mas_equalTo(10);
  60. make.trailing.mas_equalTo(-10);
  61. }];
  62. [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.leading.equalTo(self.bgView).offset(10.0);
  64. make.trailing.equalTo(self.bgView).offset(-10.0);
  65. make.bottom.mas_equalTo(-41);
  66. }];
  67. [self.detailsView mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.bottom.leading.trailing.mas_equalTo(0);
  69. make.height.mas_equalTo(30);
  70. }];
  71. }
  72. - (void)awakeFromNib {
  73. [super awakeFromNib];
  74. // Initialization code
  75. }
  76. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  77. [super setSelected:selected animated:animated];
  78. // Configure the view for the selected state
  79. }
  80. - (UIImage *)getAppIcon {
  81. NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
  82. NSString *iconName = nil;
  83. // iOS 5+ 的 AppIcon 路径解析
  84. NSDictionary *iconsDict = infoPlist[@"CFBundleIcons"];
  85. NSDictionary *primaryIcons = iconsDict[@"CFBundlePrimaryIcon"];
  86. NSArray *iconFiles = primaryIcons[@"CFBundleIconFiles"];
  87. // 获取最后一个(最大尺寸)icon
  88. iconName = iconFiles.lastObject;
  89. return [UIImage imageNamed:iconName];
  90. }
  91. - (void)fillWithData:(MOSystemNormalCellData *)data {
  92. [super fillWithData:data];
  93. _dataModel = data;
  94. [self.iconImgView setImage:[self getAppIcon]];
  95. // if([data.innerMessage.sender isEqualToString:@"10000"]){
  96. // //官方消息
  97. // [self.iconImgView setImage:[UIImage imageNamed:@"icon_im_official_news" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]];
  98. // }
  99. // else{
  100. // //官方活动
  101. // [self.iconImgView setImage:[UIImage imageNamed:@"icon_im_activity_noti" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]];
  102. // }
  103. self.contentLab.text = data.content;
  104. self.timeLab.text = [TUITool convertDateToDetailStr:data.innerMessage.timestamp];
  105. if ([self.dataModel showPicture]) {//显示图片
  106. self.pictureView.hidden = NO;
  107. [self.pictureView sd_setImageWithURL:[NSURL URLWithString:data.img]];
  108. } else {//不显示图片
  109. self.pictureView.hidden = YES;
  110. }
  111. if ([self.dataModel showTitle]) {
  112. self.titleLab.hidden = NO;
  113. self.titleLab.text = data.title;
  114. if (!self.pictureView.hidden) {//显示图片
  115. [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
  116. make.top.mas_equalTo(125);
  117. }];
  118. } else {//不显示图片
  119. [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
  120. make.top.mas_equalTo(40);
  121. }];
  122. }
  123. } else {
  124. self.titleLab.hidden = YES;
  125. }
  126. if ([self.dataModel showDetailsView]) {//显示底部detailsView
  127. self.detailsView.hidden = NO;
  128. [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
  129. make.bottom.mas_equalTo(-41);
  130. }];
  131. } else {//不显示底部detailsView
  132. self.detailsView.hidden = YES;
  133. [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
  134. make.bottom.mas_equalTo(-10);
  135. }];
  136. }
  137. if (data.linkStr.length > 0) {//富文本
  138. NSString *fullText = data.content;
  139. NSRange linkRange = [fullText rangeOfString:data.linkStr];
  140. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:fullText];
  141. if (linkRange.location != NSNotFound) {
  142. [attrStr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x0BDDFC) range:linkRange];
  143. }
  144. self.contentLab.attributedText = attrStr;
  145. self.contentLab.userInteractionEnabled = YES;
  146. // 添加点击手势
  147. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailViewClick)];
  148. [self.contentLab addGestureRecognizer:tap];
  149. } else {
  150. self.contentLab.text = data.content;
  151. }
  152. }
  153. - (void)detailViewClick {
  154. if (self.dataModel.link) {
  155. [[NSNotificationCenter defaultCenter] postNotificationName:@"MOChatJumpLink" object:self.dataModel.link];
  156. }
  157. }
  158. - (void)layoutSubviews {
  159. [super layoutSubviews];
  160. }
  161. #pragma mark - TUIMessageCellProtocol
  162. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  163. MOSystemNormalCellData *evaluationCellData = (MOSystemNormalCellData *)data;
  164. CGFloat topTitleHeight = 15.0 / 2.0 + 8.0 + 24.0 + 8.0;
  165. CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0;
  166. CGFloat contentWidth = viewWidth - 10.0 * 2.0;
  167. CGRect contentRect;
  168. if(evaluationCellData.type == 9){
  169. contentRect = [evaluationCellData.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  170. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  171. attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]}
  172. context:nil];
  173. contentRect.size.height = contentRect.size.height + topTitleHeight + 10.0 + 15.0 / 2.0;
  174. }
  175. CGSize size = CGSizeMake(245, ceilf(contentRect.size.height));
  176. return size;
  177. }
  178. #pragma mark - Lazy
  179. - (UIView *)bgView{
  180. if (_bgView == nil){
  181. _bgView = [UIView new];
  182. _bgView.layer.cornerRadius = 12.0;
  183. _bgView.layer.masksToBounds = YES;
  184. _bgView.backgroundColor = [UIColor whiteColor];
  185. }
  186. return _bgView;
  187. }
  188. - (UIImageView *)iconImgView{
  189. if (!_iconImgView){
  190. _iconImgView = [[UIImageView alloc] init];
  191. _iconImgView.userInteractionEnabled = NO;
  192. _iconImgView.layer.cornerRadius = 6;
  193. _iconImgView.layer.masksToBounds = YES;
  194. }
  195. return _iconImgView;
  196. }
  197. - (UILabel *)groupLabel {
  198. if (!_groupLabel) {
  199. _groupLabel = [UILabel new];
  200. _groupLabel.textColor = UIColorFromHex(0x666666);
  201. _groupLabel.textAlignment = NSTextAlignmentLeft;
  202. _groupLabel.font = [MOSystemNormalCell getTheFontWithSize:13.0 AndFontName:@"Roboto"];
  203. _groupLabel.text = @"MiMo Team";
  204. }
  205. return _groupLabel;
  206. }
  207. - (UILabel *)titleLab{
  208. if(!_titleLab){
  209. _titleLab = [UILabel new];
  210. _titleLab.numberOfLines = 0;
  211. _titleLab.textColor = UIColorFromHex(0x666666);
  212. _titleLab.textAlignment = NSTextAlignmentLeft;
  213. _titleLab.font = [MOSystemNormalCell getTheFontWithSize:15.0 AndFontName:@"Roboto-Bold"];
  214. }
  215. return _titleLab;
  216. }
  217. - (UILabel *)timeLab{
  218. if(!_timeLab){
  219. _timeLab = [UILabel new];
  220. _timeLab.textColor = UIColorFromHex(0x333333);
  221. _timeLab.textAlignment = NSTextAlignmentRight;
  222. _timeLab.font = [MOSystemNormalCell getTheFontWithSize:12.0 AndFontName:@"Roboto"];
  223. }
  224. return _timeLab;
  225. }
  226. - (UIImageView *)pictureView {
  227. if (!_pictureView) {
  228. _pictureView = [[UIImageView alloc] init];
  229. _pictureView.contentMode = UIViewContentModeScaleAspectFill;
  230. _pictureView.layer.masksToBounds = YES;
  231. _pictureView.layer.cornerRadius = 12;
  232. }
  233. return _pictureView;
  234. }
  235. - (UILabel *)contentLab{
  236. if(!_contentLab){
  237. _contentLab = [UILabel new];
  238. _contentLab.textColor = UIColorFromHex(0x333333);
  239. _contentLab.textAlignment = NSTextAlignmentLeft;
  240. _contentLab.font = [MOSystemNormalCell getTheFontWithSize:13.0 AndFontName:@"Roboto"];
  241. _contentLab.numberOfLines = 0;
  242. }
  243. return _contentLab;
  244. }
  245. - (UIView *)detailsView {
  246. if (!_detailsView) {
  247. _detailsView = [[UIView alloc] init];
  248. UIView *lineView = [[UIView alloc] init];
  249. lineView.backgroundColor = UIColorFromHex(0xEEEEEE);
  250. [_detailsView addSubview:lineView];
  251. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  252. make.top.mas_equalTo(0);
  253. make.left.mas_equalTo(10);
  254. make.right.mas_equalTo(-10);
  255. make.height.mas_equalTo(1);
  256. }];
  257. UILabel *detailLabel = [[UILabel alloc] init];
  258. detailLabel.textColor = UIColorFromHex(0x0BDDFC);
  259. detailLabel.font = [MOSystemNormalCell getTheFontWithSize:12.0 AndFontName:@"Roboto"];
  260. detailLabel.text = @"View details";
  261. [_detailsView addSubview:detailLabel];
  262. [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  263. make.left.mas_equalTo(10);
  264. make.centerY.mas_equalTo(0);
  265. }];
  266. UIImageView *arrowImageView = [[UIImageView alloc] init];
  267. [arrowImageView setImage:[UIImage imageNamed:@"icon_im_arrow" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]];
  268. [_detailsView addSubview:arrowImageView];
  269. [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  270. make.right.mas_equalTo(-10);
  271. make.centerY.mas_equalTo(0);
  272. make.size.mas_equalTo(CGSizeMake(12, 12));
  273. }];
  274. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailViewClick)];
  275. [_detailsView addGestureRecognizer:tap];
  276. }
  277. return _detailsView;
  278. }
  279. #pragma mark - 类方法
  280. + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
  281. UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
  282. if(!customFont){
  283. customFont = [MOSystemNormalCell MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
  284. }
  285. return customFont;
  286. }
  287. + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
  288. bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
  289. UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
  290. UIFontDescriptorSymbolicTraits symbolicTraits = 0;
  291. if (italic) {
  292. symbolicTraits |= UIFontDescriptorTraitItalic;
  293. }
  294. if (bold) {
  295. symbolicTraits |= UIFontDescriptorTraitBold;
  296. }
  297. UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
  298. return specialFont;
  299. }
  300. @end