| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- //
- // MOUserNotificationCell.m
- // TUIChat
- //
- // Created by SuperC on 2025/10/13.
- //
- #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]
- #import "MOUserNotificationCell.h"
- @implementation MOUserNotificationCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if(self){
- self.backgroundColor = [UIColor clearColor];
- self.contentView.backgroundColor = [UIColor clearColor];
- //Cell 去除选中效果
- self.selectionStyle = UITableViewCellSelectionStyleNone;
-
- [self.contentView addSubview:self.titleLab];
- [self.contentView addSubview:self.contentLab];
-
- [self setupView];
- }
- return self;
- }
- - (void)setupView{
- [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.leading.equalTo(self.contentView).offset(25.0);
- make.trailing.equalTo(self.contentView).offset(-25.0);
- make.top.equalTo(self.contentView).offset(8.0);
- }];
-
- [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.leading.equalTo(self.contentView).offset(25.0);
- make.trailing.equalTo(self.contentView).offset(-25.0);
- make.top.equalTo(self.titleLab.mas_bottom).offset(2.0);
- }];
- }
- - (void)fillWithData:(MOUserNotificationCellData *)data{
- [super fillWithData:data];
-
- _dataModel = data;
-
- self.titleLab.text = data.title;
- self.contentLab.text = data.content;
-
- // tell constraints they need updating
- [self setNeedsUpdateConstraints];
- // update constraints now so we can animate the change
- [self updateConstraintsIfNeeded];
- [self layoutIfNeeded];
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
- // Initialization code
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- - (UILabel *)titleLab{
- if(!_titleLab){
- _titleLab = [UILabel new];
- _titleLab.numberOfLines = 0;
- _titleLab.textColor = UIColorFromHex(0x17171A);
- _titleLab.textAlignment = NSTextAlignmentCenter;
- _titleLab.font = [MOUserNotificationCell getTheFontWithSize:12.0 AndFontName:@"Inter-Regular_Medium"];
- }
- return _titleLab;
- }
- - (UILabel *)contentLab{
- if(!_contentLab){
- _contentLab = [UILabel new];
- _contentLab.textColor = UIColorFromHex(0x878A99);
- _contentLab.textAlignment = NSTextAlignmentCenter;
- _contentLab.font = [MOUserNotificationCell getTheFontWithSize:10.0 AndFontName:@"Inter-Regular"];
- _contentLab.numberOfLines = 0;
- }
- return _contentLab;
- }
- #pragma mark - TUIMessageCellProtocol
- + (CGSize)getContentSize:(TUIMessageCellData *)data {
-
- MOUserNotificationCellData *evaluationCellData = (MOUserNotificationCellData *)data;
-
- CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0;
- CGFloat contentWidth = viewWidth - 10.0 * 2.0;
-
- //标题以上固定距离
- CGFloat titleTopSpacing = 8.0;
-
- //标题高度
- CGFloat titleHeight = 0;
- if (evaluationCellData.title.length > 0) {
- UIFont *titleFont = [self getTheFontWithSize:12.0 AndFontName:@"Inter-Regular_Medium"];
- titleHeight = [evaluationCellData.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
- options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont}
- context:nil].size.height + 2;//2是补偿高度
-
- }
-
- //内容高度
- CGFloat contentHeight = 0;
- if(evaluationCellData.content.length > 0){
- UIFont *contentFont = [self getTheFontWithSize:10.0 AndFontName:@"Inter-Regular"];
- contentHeight = [evaluationCellData.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
- options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
- attributes:@{NSFontAttributeName : contentFont}
- context:nil].size.height;
- }
-
- //30是bgView上下距离
- CGFloat bgViewBottomSpacing = 8.0;
-
- CGFloat totalHeight = titleTopSpacing + titleHeight + 2.0 + contentHeight + bgViewBottomSpacing;
-
- CGSize size = CGSizeMake(245, ceilf(totalHeight));
- return size;
- }
- #pragma mark - 类方法
- + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
- UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
- if(!customFont){
- customFont = [MOUserNotificationCell MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
- }
- return customFont;
- }
- + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
- bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
- UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
- UIFontDescriptorSymbolicTraits symbolicTraits = 0;
- if (italic) {
- symbolicTraits |= UIFontDescriptorTraitItalic;
- }
- if (bold) {
- symbolicTraits |= UIFontDescriptorTraitBold;
- }
- UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
- return specialFont;
- }
- @end
|