// // PIUniversalBannerView.m // YuMi // // Created by duoban on 2024/3/19. // #import #import "YUMIMacroUitls.h" #import "DJDKMIMOMColor.h" #import "PIUniversalBannerView.h" #import #import "DJDKMIMOMColor.h" #import "YuMi-swift.h" @interface PIUniversalBannerView() @property (strong, nonatomic) SVGAParser *parser; @property (nonatomic,strong) SVGAImageView *svgaView; @property (nonatomic,strong) UIImageView *bgImageView; @property (nonatomic,strong) UILabel *titleView; @property (nonatomic,strong) UIButton *clickBtn; @property(nonatomic,strong) SVGAVideoEntity *videoItem; @end @implementation PIUniversalBannerView -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if(self){ [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.bgImageView]; [self addSubview:self.svgaView]; [self addSubview:self.titleView]; [self addSubview:self.clickBtn]; } - (void)initSubViewConstraints { [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.svgaView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(self); make.leading.trailing.equalTo(self).inset(kGetScaleWidth(70)); }]; [self.clickBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; } -(void)setModel:(PIUniversalBannerModel *)model{ _model = model; NSDictionary *textDic = model.template; if(textDic.allKeys.count == 0)return ; NSString *title = textDic[@"zh-CHT"] == nil ? textDic[textDic.allKeys.firstObject] : textDic[@"zh-CHT"]; if(title.length == 0)return; [title stringByReplacingOccurrencesOfString:@"\n" withString:@""]; CGFloat font = _isSvga == YES ? _model.fontSize / 2 : _model.fontSize; NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font],NSForegroundColorAttributeName:[DJDKMIMOMColor colorWithHexString:_model.textColor]}]; for (PIUniversalBannerItemModel *model in _model.contents) { if([model.type isEqualToString:@"TEXT"]){ NSDictionary *subTextDic = model.text; if(subTextDic.allKeys.count > 0){ NSString *subText = subTextDic[@"zh-CHT"] == nil ? subTextDic[subTextDic.allKeys.firstObject] : subTextDic[@"zh-CHT"]; NSAttributedString *attText = [[NSAttributedString alloc]initWithString:subText attributes:@{NSForegroundColorAttributeName:[DJDKMIMOMColor colorWithHexString:model.textColor],NSFontAttributeName:[UIFont systemFontOfSize:font]}]; if ([attribute.string containsString:[NSString stringWithFormat:@"{%@}",model.key]]){ [attribute replaceCharactersInRange:[attribute.string rangeOfString:[NSString stringWithFormat:@"{%@}",model.key]] withAttributedString:attText]; } } } } if(_isSvga == YES){ self.bgImageView.hidden = YES; self.svgaView.hidden = NO; self.titleView.hidden = NO; self.svgaView.loops = 1; self.titleView.attributedText = attribute; self.svgaView.clearsAfterStop = NO; self.svgaView.videoItem = _model.videoItem; // [self.svgaView setAttributedText:attribute forKey:_model.svgaTextKey]; [self.svgaView startAnimation]; [self.titleView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(kGetScaleWidth(self.model.resourceTop)); make.leading.trailing.equalTo(self).inset(kGetScaleWidth(70)); }]; }else{ self.bgImageView.hidden = NO; self.bgImageView.image = _model.image; self.titleView.hidden = NO; self.svgaView.hidden = YES; self.titleView.attributedText = attribute; } } -(void)clickAction{ if(self.delegate && [self.delegate respondsToSelector:@selector(pIUniversalBannerView:didClick:)]){ [self.delegate pIUniversalBannerView:self didClick:self.model]; } } #pragma mark -懒加载 - (UIImageView *)bgImageView{ if (!_bgImageView){ _bgImageView = [UIImageView new]; _bgImageView.userInteractionEnabled = YES; } return _bgImageView; } -(UILabel *)titleView{ if (!_titleView){ _titleView = [UILabel new]; _titleView.textAlignment = NSTextAlignmentCenter; _titleView.numberOfLines = 2; } return _titleView; } - (SVGAParser *)parser { if (!_parser) { _parser = [[SVGAParser alloc]init]; } return _parser; } - (SVGAImageView *)svgaView { if (!_svgaView) { _svgaView= [[SVGAImageView alloc]init]; _svgaView.backgroundColor = [UIColor clearColor]; _svgaView.userInteractionEnabled = YES; } return _svgaView; } -(UIButton *)clickBtn{ if (!_clickBtn){ _clickBtn = [UIButton new]; [_clickBtn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside]; } return _clickBtn; } @end