MOUserNotificationCell.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // MOUserNotificationCell.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 "MOUserNotificationCell.h"
  9. @implementation MOUserNotificationCell
  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.titleLab];
  18. [self.contentView addSubview:self.contentLab];
  19. [self setupView];
  20. }
  21. return self;
  22. }
  23. - (void)setupView{
  24. [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  25. make.leading.equalTo(self.contentView).offset(25.0);
  26. make.trailing.equalTo(self.contentView).offset(-25.0);
  27. make.top.equalTo(self.contentView).offset(8.0);
  28. }];
  29. [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.leading.equalTo(self.contentView).offset(25.0);
  31. make.trailing.equalTo(self.contentView).offset(-25.0);
  32. make.top.equalTo(self.titleLab.mas_bottom).offset(2.0);
  33. }];
  34. }
  35. - (void)fillWithData:(MOUserNotificationCellData *)data{
  36. [super fillWithData:data];
  37. _dataModel = data;
  38. self.titleLab.text = data.title;
  39. self.contentLab.text = data.content;
  40. // tell constraints they need updating
  41. [self setNeedsUpdateConstraints];
  42. // update constraints now so we can animate the change
  43. [self updateConstraintsIfNeeded];
  44. [self layoutIfNeeded];
  45. }
  46. - (void)awakeFromNib {
  47. [super awakeFromNib];
  48. // Initialization code
  49. }
  50. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  51. [super setSelected:selected animated:animated];
  52. // Configure the view for the selected state
  53. }
  54. - (UILabel *)titleLab{
  55. if(!_titleLab){
  56. _titleLab = [UILabel new];
  57. _titleLab.numberOfLines = 0;
  58. _titleLab.textColor = UIColorFromHex(0x17171A);
  59. _titleLab.textAlignment = NSTextAlignmentCenter;
  60. _titleLab.font = [MOUserNotificationCell getTheFontWithSize:12.0 AndFontName:@"Inter-Regular_Medium"];
  61. }
  62. return _titleLab;
  63. }
  64. - (UILabel *)contentLab{
  65. if(!_contentLab){
  66. _contentLab = [UILabel new];
  67. _contentLab.textColor = UIColorFromHex(0x878A99);
  68. _contentLab.textAlignment = NSTextAlignmentCenter;
  69. _contentLab.font = [MOUserNotificationCell getTheFontWithSize:10.0 AndFontName:@"Inter-Regular"];
  70. _contentLab.numberOfLines = 0;
  71. }
  72. return _contentLab;
  73. }
  74. #pragma mark - TUIMessageCellProtocol
  75. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  76. MOUserNotificationCellData *evaluationCellData = (MOUserNotificationCellData *)data;
  77. CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0;
  78. CGFloat contentWidth = viewWidth - 10.0 * 2.0;
  79. //标题以上固定距离
  80. CGFloat titleTopSpacing = 8.0;
  81. //标题高度
  82. CGFloat titleHeight = 0;
  83. if (evaluationCellData.title.length > 0) {
  84. UIFont *titleFont = [self getTheFontWithSize:12.0 AndFontName:@"Inter-Regular_Medium"];
  85. titleHeight = [evaluationCellData.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  86. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont}
  87. context:nil].size.height + 2;//2是补偿高度
  88. }
  89. //内容高度
  90. CGFloat contentHeight = 0;
  91. if(evaluationCellData.content.length > 0){
  92. UIFont *contentFont = [self getTheFontWithSize:10.0 AndFontName:@"Inter-Regular"];
  93. contentHeight = [evaluationCellData.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  94. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  95. attributes:@{NSFontAttributeName : contentFont}
  96. context:nil].size.height;
  97. }
  98. //30是bgView上下距离
  99. CGFloat bgViewBottomSpacing = 8.0;
  100. CGFloat totalHeight = titleTopSpacing + titleHeight + 2.0 + contentHeight + bgViewBottomSpacing;
  101. CGSize size = CGSizeMake(245, ceilf(totalHeight));
  102. return size;
  103. }
  104. #pragma mark - 类方法
  105. + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
  106. UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
  107. if(!customFont){
  108. customFont = [MOUserNotificationCell MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
  109. }
  110. return customFont;
  111. }
  112. + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
  113. bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
  114. UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
  115. UIFontDescriptorSymbolicTraits symbolicTraits = 0;
  116. if (italic) {
  117. symbolicTraits |= UIFontDescriptorTraitItalic;
  118. }
  119. if (bold) {
  120. symbolicTraits |= UIFontDescriptorTraitBold;
  121. }
  122. UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
  123. return specialFont;
  124. }
  125. @end