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

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

由于标签是从开始显示的,文字的内容长度又不固定,图片的位置相对固定。

想实现这样的效果需要计算出文字和图片的总长度,通过设置分段文字颜色的富文本来实现:

-(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];
}
目录
相关文章
|
1月前
如何实现带背景的镂空文字
如何实现带背景的镂空文字
20 1
|
索引
echarts x轴文字显示不全(xAxis文字倾斜比较全面的3种做法值得推荐)
echarts x轴标签文字过多导致显示不全 如图: 解决办法1:xAxis.axisLabel 属性 axisLabel的类型是object ,主要作用是:坐标轴刻度标签的相关设置。(当然yAxis也是一样有这个属性的) ...
4059 0
测试图片随文字上下居中
测试图片随文字上下居中
|
前端开发 JavaScript
NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单
用不到80行代码复原了NaiveUI中的渐变文字组件,内容不多,非常简单,小小娱乐一次。。。。。。。
339 0
|
架构师 开发者
图片和文字控件|学习笔记
快速学习图片和文字控件。
76 0
|
前端开发 iOS开发
文字样式处理总结
文字样式处理总结
文字样式处理总结
|
前端开发 程序员 开发者
文本阴影 | 学习笔记
快速学习文本阴影。
117 0
|
前端开发 Android开发
利用PorterDuffXfermode绘制图片文字
PorterDuffXfermode是Android中用来对图层进行操作的类,类似于数学中的交集、并集,将上层(src)和下层(dst)进行特定的方式进行混合显示。
1023 0
|
C#
C# 设置Word文档背景(纯色/渐变/图片背景)
Word是我们日常生活、学习和工作中必不可少的文档处理工具。精致美观的文档能给人带来阅读时视觉上的美感。在本篇文章中,将介绍如何使用组件Free Spire.Doc for .NET(社区版)给Word设置文档背景。
1234 0