| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- //
- // MOBaseLevelView.m
- // MiMoLive
- //
- // Created by SuperC on 2025/1/7.
- //
- #import "MOBaseLevelView.h"
- #import <UIImageView+YYWebImage.h>
- #import <YYAnimatedImageView.h>
- #import <objc/runtime.h>
- @interface MOBaseLevelView ()
- @property (nonatomic, strong) UIImageView *levelImg;
- @property (nonatomic, strong) UILabel *levelLabel;
- @end
- @implementation MOBaseLevelView
- - (instancetype)init{
- self = [super init];
- if(self){
- [self setupUI];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if(self){
- [self setupUI];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)coder{
- self = [super initWithCoder:coder];
- if(self){
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI{
- CGFloat levelWidth = MOLevelNormalWidth;
- CGFloat levelLabelX = 16.0;
- CGFloat levelLabexWidth = levelWidth - levelLabelX;
- self.backgroundColor = [UIColor clearColor];
-
- [self addSubview:self.levelImg];
- [self.levelImg mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.bottom.right.top.equalTo(self);
- }];
-
- self.levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(levelLabelX, 0, levelLabexWidth, 16.0)];
- self.levelLabel.font = [MOTextTools poppinsExtraBoldFont:10.0];
- self.levelLabel.textColor = [UIColor whiteColor];
- self.levelLabel.textAlignment = NSTextAlignmentCenter;
- self.levelLabel.text = @" ";
- [self addSubview:self.levelLabel];
- [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self).offset(levelLabelX);
- make.top.bottom.equalTo(self);
- make.right.equalTo(self).offset(0.0);
- }];
- }
- - (void)setLevelNum:(NSInteger)levelNum{
- if(levelNum == 0){
- levelNum = 1;
- }
-
- _levelNum = levelNum;
-
- NSArray *levelInfoArr = [MOSvgaSourceManage shareManager].allLevelSoureArr;
- MOStoreInfo *medalInfo;
- for (MOStoreInfo *object in levelInfoArr) {
- if(levelNum >= object.limit){
- medalInfo = object;
- break;
- }
- }
-
- WEAKSELF
- if(medalInfo.res.length > 0){
- NSURL *url = [NSURL URLWithString:medalInfo.res];
- [self.levelImg sd_setImageWithURL:url];
- }
- else{
- UIImage *levelImg = [MOTextTools createLevelImageViewWith:levelNum];
- [self.levelImg setImage:levelImg];
- }
-
- self.levelLabel.text = [NSString stringWithFormat:@"%zd",levelNum];
-
- if(levelNum >= 100){
- [self.levelLabel mas_updateConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self).offset(-5.0);
- }];
- }
- else if (levelNum < 10){
- [self.levelLabel mas_updateConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self).offset(-2.0);
- }];
- }
- else{
- [self.levelLabel mas_updateConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self).offset(-2.0);
- }];
- }
- }
- - (UIImageView *)levelImg{
- if(!_levelImg){
-
- CGFloat levelWidth = MOLevelNormalWidth;
- _levelImg = [[UIImageView alloc] init];
- _levelImg.frame = CGRectMake(0.0, 0.0, levelWidth, 16.0);
- _levelImg.contentMode = UIViewContentModeScaleAspectFit;
- }
- return _levelImg;
- }
- #pragma mark - Public Methods
- + (void)setStyledTextForLabel:(UILabel *)label
- fullText:(NSString *)text
- targetText:(NSString *)targetText
- color:(UIColor *)color
- hasUnderline:(BOOL)hasUnderline {
-
- if (!label || !text || !targetText) {
- return;
- }
-
- NSMutableAttributedString *attributedString;
-
- // 检查是否已有富文本,如果有则在现有基础上修改,否则创建新的
- if (label.attributedText && label.attributedText.length > 0) {
- // 验证文本内容是否一致
- if ([label.attributedText.string isEqualToString:text]) {
- // 文本一致,在现有富文本基础上修改
- attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
- } else {
- // 文本不一致,创建新的富文本
- attributedString = [[NSMutableAttributedString alloc] initWithString:text];
- // 设置整体文本样式
- [attributedString addAttribute:NSFontAttributeName
- value:[UIFont systemFontOfSize:14.0]
- range:NSMakeRange(0, text.length)];
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor blackColor]
- range:NSMakeRange(0, text.length)];
- }
- } else {
- // 没有现有富文本,创建新的
- attributedString = [[NSMutableAttributedString alloc] initWithString:text];
- // 设置整体文本样式
- [attributedString addAttribute:NSFontAttributeName
- value:[UIFont systemFontOfSize:14.0]
- range:NSMakeRange(0, text.length)];
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor blackColor]
- range:NSMakeRange(0, text.length)];
- }
-
- // 查找目标文本的位置
- NSRange targetRange = [attributedString.string rangeOfString:targetText];
- if (targetRange.location != NSNotFound) {
- // 设置目标文本颜色
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:color
- range:targetRange];
-
- // 添加下划线(如果需要)
- if (hasUnderline) {
- [attributedString addAttribute:NSUnderlineStyleAttributeName
- value:@(NSUnderlineStyleSingle)
- range:targetRange];
- [attributedString addAttribute:NSUnderlineColorAttributeName
- value:color
- range:targetRange];
- } else {
- // 如果不需要下划线,移除可能存在的下划线样式
- [attributedString removeAttribute:NSUnderlineStyleAttributeName range:targetRange];
- [attributedString removeAttribute:NSUnderlineColorAttributeName range:targetRange];
- }
-
- // 存储目标文本范围信息(支持多个范围)
- NSMutableArray *ranges = objc_getAssociatedObject(label, @"targetRanges");
- if (!ranges) {
- ranges = [[NSMutableArray alloc] init];
- }
- [ranges addObject:[NSValue valueWithRange:targetRange]];
- objc_setAssociatedObject(label, @"targetRanges", ranges, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
-
- label.attributedText = attributedString;
- }
- + (void)addTapGestureToLabel:(UILabel *)label
- target:(id)target
- action:(SEL)action {
-
- if (!label || !target || !action) {
- return;
- }
-
- // 确保label可以接收用户交互
- label.userInteractionEnabled = YES;
-
- // 移除已存在的手势(避免重复添加)
- for (UIGestureRecognizer *gesture in label.gestureRecognizers) {
- if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
- [label removeGestureRecognizer:gesture];
- }
- }
-
- // 添加点击手势
- UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
- [label addGestureRecognizer:tapGesture];
-
- // 存储相关信息
- objc_setAssociatedObject(label, @"target", target, OBJC_ASSOCIATION_ASSIGN);
- objc_setAssociatedObject(label, @"action", NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- + (void)setMultipleStyledTextForLabel:(UILabel *)label
- fullText:(NSString *)fullText
- styleConfigs:(NSArray<NSDictionary *> *)styleConfigs {
-
- if (!label || !fullText || !styleConfigs || styleConfigs.count == 0) {
- return;
- }
-
- // 创建初始富文本
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullText];
-
- // 设置整体文本样式
- [attributedString addAttribute:NSFontAttributeName
- value:[UIFont systemFontOfSize:14.0]
- range:NSMakeRange(0, fullText.length)];
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:[UIColor blackColor]
- range:NSMakeRange(0, fullText.length)];
-
- NSMutableArray *ranges = [[NSMutableArray alloc] init];
-
- // 遍历所有样式配置
- for (NSDictionary *config in styleConfigs) {
- NSString *targetText = config[@"targetText"];
- UIColor *color = config[@"color"];
- NSNumber *hasUnderlineNum = config[@"hasUnderline"];
-
- if (!targetText || !color || !hasUnderlineNum) {
- continue; // 跳过无效配置
- }
-
- BOOL hasUnderline = [hasUnderlineNum boolValue];
-
- // 查找目标文本的位置
- NSRange targetRange = [fullText rangeOfString:targetText];
- if (targetRange.location != NSNotFound) {
- // 设置目标文本颜色
- [attributedString addAttribute:NSForegroundColorAttributeName
- value:color
- range:targetRange];
-
- // 添加或移除下划线
- if (hasUnderline) {
- [attributedString addAttribute:NSUnderlineStyleAttributeName
- value:@(NSUnderlineStyleSingle)
- range:targetRange];
- [attributedString addAttribute:NSUnderlineColorAttributeName
- value:color
- range:targetRange];
- } else {
- [attributedString removeAttribute:NSUnderlineStyleAttributeName range:targetRange];
- [attributedString removeAttribute:NSUnderlineColorAttributeName range:targetRange];
- }
-
- // 收集范围信息
- [ranges addObject:[NSValue valueWithRange:targetRange]];
- }
- }
-
- // 存储所有范围信息
- objc_setAssociatedObject(label, @"targetRanges", ranges, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
-
- // 设置富文本
- label.attributedText = attributedString;
- }
- + (UILabel *)createFeedbackLabelWithText:(NSString *)text
- feedbackText:(NSString *)feedbackText
- target:(id)target
- action:(SEL)action {
-
- UILabel *label = [[UILabel alloc] init];
- label.numberOfLines = 0;
-
- // 使用新的分离方法设置样式
- [self setStyledTextForLabel:label
- fullText:text
- targetText:feedbackText
- color:[UIColor blueColor]
- hasUnderline:YES];
-
- // 使用新的分离方法添加点击事件
- [self addTapGestureToLabel:label target:target action:action];
-
- return label;
- }
- @end
|