如何实现图片多种颜色的文字混排并且带的背景颜色范围比文字图片范围大

由于标签是从开始显示的,文字的内容长度又不固定,图片的位置相对固定。
想实现这样的效果需要计算出文字和图片的总长度,通过设置分段文字颜色的富文本来实现:

-(void)setRank:(NSUInteger) rank
{
    _rank = rank;
    UIColor *strokeColor = BGColorHex(C69116);
    NSUInteger tempRank = rank/10+1;
    if(0 == rank)
    {
        tempRank = 0;
    }
    else if(rank >= 100)
    {
        tempRank = 10;
    }
    switch (tempRank) {
        case 0:
            strokeColor = BGColorHex(A1A1A1);
            break;
        case 1:
            strokeColor = BGColorHex(FE7241);
            break;
        case 2:
            strokeColor = BGColorHex(FF9802);
            break;
        case 3:
            strokeColor = BGColorHex(FFBF01);
            break;
        case 4:
            strokeColor = BGColorHex(CBDC3C);
            break;
        case 5:
            strokeColor = BGColorHex(699E38);
            break;
        case 6:
            strokeColor = BGColorHex(26A99C);
            break;
        case 7:
            strokeColor = BGColorHex(05A8F3);
            break;
        case 8:
            strokeColor = BGColorHex(4F6BEA);
            break;
        case 9:
            strokeColor = BGColorHex(663CB5);
            break;
        case 10:
            strokeColor = BGColorHex(E9407B);
            break;
            
        default:
            break;
    }
    
    NSString *rankStr = [NSString stringWithFormat:@"%lu", rank];
    NSString *str = [NSString stringWithFormat:@"fron%@", rankStr];
    NSUInteger fillCharacterCount = 4;
    NSMutableAttributedString *textAttributedString = [[NSMutableAttributedString alloc] initWithString:str];
    [textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, fillCharacterCount)];
    [textAttributedString addAttribute:NSStrokeColorAttributeName value:strokeColor range:NSMakeRange(fillCharacterCount, rankStr.length)];
    [textAttributedString addAttribute:NSStrokeWidthAttributeName value:@4.0 range:NSMakeRange(fillCharacterCount, rankStr.length)];
   [textAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, fillCharacterCount)];
   [textAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12.f] range:NSMakeRange(fillCharacterCount, rankStr.length)];
    self.describeTitleLabel.attributedText = textAttributedString;
    [self.describeTitleLabel updateLayout];
    
    textAttributedString = [[NSMutableAttributedString alloc] initWithString:str];
    [textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, fillCharacterCount)];
    [textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(fillCharacterCount, rankStr.length)];
    [textAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, fillCharacterCount)];
    [textAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12.f] range:NSMakeRange(fillCharacterCount, rankStr.length)];
    self.underDescribeTitleLabel.attributedText = textAttributedString;
    [self.underDescribeTitleLabel updateLayout];
    
    
    NSString *filename = [NSString stringWithFormat:@"lv%lu", tempRank];
    self.decribeImageView.image = [UIImage imageNamed:filename];
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值