// // XPRoomCandyGiftView.m // xplan-ios // // Created by 冯硕 on 2021/12/10. // #import "XPRoomCandyGiftView.h" ///Third #import #import #import "ThemeColor+Room.h" #import "CandyTreeResultModel.h" #import "NSObject+MJExtension.h" @interface XPRoomCandyGiftView () ///动画管理类 @property (strong, nonatomic) SVGAParser *parser; ///糖果树特效 @property (nonatomic,strong) SVGAImageView *candyTreeView; ///背景图 @property (nonatomic,strong) UIImageView *backImageView; ///显示文本内容 @property (nonatomic,strong) UILabel *titleLabel; @end @implementation XPRoomCandyGiftView - (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.candyTreeView]; [self addSubview:self.backImageView]; [self addSubview:self.titleLabel]; } - (void)initSubViewConstraints { [self.candyTreeView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.top.mas_equalTo(self); make.height.mas_equalTo(kGetScaleWidth(55)); }]; [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.top.mas_equalTo(self.candyTreeView); make.height.mas_equalTo(kGetScaleWidth(55)); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.backImageView); }]; } - (NSAttributedString *)createAttribute:(NSString * )text color:(UIColor *)color fontSize:(CGFloat)fonSize { if(text.length == 0)return nil; NSDictionary * attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:fonSize], NSForegroundColorAttributeName:color}; NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text attributes:attribute]; return attr; } #pragma mark - Getters And Setters - (void)setCandyInfo:(NSDictionary *)candyInfo { if (candyInfo) { CandyTreeGiftInfoModel * giftInfo = [CandyTreeGiftInfoModel modelWithDictionary:candyInfo]; NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init]; if(giftInfo.nick.length > 5){ giftInfo.nick = [NSString stringWithFormat:@"%@...",[giftInfo.nick substringToIndex:5]]; } CGFloat fontSize = self.isMaxLargeGift ? 20 : 12; [attribute appendAttributedString:[self createAttribute:YMLocalizedString(@"XPRoomCandyGiftView0") color:[UIColor whiteColor] fontSize:fontSize]]; [attribute appendAttributedString:[self createAttribute:giftInfo.nick color:UIColorFromRGB(0xFEF23E) fontSize:fontSize]]; [attribute appendAttributedString:[self createAttribute:YMLocalizedString(@"XPRoomCandyGiftView1") color:[UIColor whiteColor] fontSize:fontSize]]; [attribute appendAttributedString:[self createAttribute:giftInfo.prizeName color:UIColorFromRGB(0x00EAFF) fontSize:fontSize]]; if (giftInfo.prizeNum > 1) { [attribute appendAttributedString:[self createAttribute:[NSString stringWithFormat:@" X%d", giftInfo.prizeNum] color:UIColorFromRGB(0x00EAFF) fontSize:fontSize]]; } if (self.isMaxLargeGift) { self.backImageView.hidden = YES; self.candyTreeView.hidden = NO; @kWeakify(self); NSString * anatomiser1Name = [NSString stringWithFormat:@"%@/candyTree_banner.svga", API_Image_URL]; [self.parser parseWithURL:[NSURL URLWithString:anatomiser1Name] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) { @kStrongify(self); self.candyTreeView.loops = 1; self.candyTreeView.clearsAfterStop = NO; self.candyTreeView.videoItem = videoItem; [self.candyTreeView setAttributedText:attribute forKey:@"noble_text_tx"]; [self.candyTreeView startAnimation]; } failureBlock:^(NSError * _Nonnull error) { }]; } else { self.backImageView.hidden = NO; self.candyTreeView.hidden = YES; self.titleLabel.attributedText = attribute; self.titleLabel.textAlignment = NSTextAlignmentCenter; } } } - (SVGAImageView *)candyTreeView { if (!_candyTreeView) { _candyTreeView = [[SVGAImageView alloc]init]; _candyTreeView.backgroundColor = [UIColor clearColor]; _candyTreeView.userInteractionEnabled = NO; } return _candyTreeView; } - (SVGAParser *)parser { if (!_parser) { _parser = [[SVGAParser alloc]init]; } return _parser; } - (UIImageView *)backImageView { if (!_backImageView) { _backImageView = [[UIImageView alloc] init]; _backImageView.userInteractionEnabled = YES; _backImageView.image = [UIImage imageNamed:@"room_candytree_large_gift_bg"]; } return _backImageView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textAlignment = NSTextAlignmentCenter; } return _titleLabel; } @end