iOS开发笔记-一种任意字体、颜色混排UILabel的实现

最近开发新App,射妓狮给的图上出现一种不同大小字体混排的Label,就像下面这种:

 

想了想,最简单的方法是使用多个UILabel排列显示,但是这样不仅麻烦而且效果也不好,索性自定义UILabel来尽可能的满足使用灵活性。

 

实现方法

 

与正常自定义控件的方法类似,主要利用了CoreGraphics来动态绘制字体,但这里字体的参数都用NSArray存储,以尽最大可能不受具体内容约束,实现灵活性。

代码如下:

 

复制代码
#import <UIKit/UIKit.h>

@interface UnevenHeightLabel : UIView
@property (nonatomic) NSArray *strings;
@property (nonatomic) NSArray *fonts;
@property (nonatomic) NSArray *originY;
@property (nonatomic) NSArray *fontColors;

-(instancetype) initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts originY:(NSArray *)originY  stringColors:(NSArray *) colors;
@end
复制代码

 

 

复制代码
#import "UnevenHeightLabel.h"
#import "UIColor+HexColor.h"

@implementation UnevenHeightLabel


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 1.0f);
    CGRect startRect;
    CGSize requiredSize;
    float sumWidth=0;
    if(_strings!=nil&& _strings.count>0){
        for (int i=0; i<_strings.count; i++) {
            CGSize maxSize=rect.size;
            requiredSize=[_strings[i] boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_fonts[i]} context:nil].size;
            if(i==0){
                startRect=CGRectMake(0, 0, maxSize.width, maxSize.height);
            }
            else{
                startRect=CGRectMake(sumWidth, [_originY[i] floatValue], requiredSize.width, requiredSize.height);
                
            }
            [_strings[i] drawInRect:startRect
                     withAttributes:@{NSFontAttributeName:_fonts[i],
                                      NSForegroundColorAttributeName:_fontColors[i]}];
            
            sumWidth=sumWidth+requiredSize.width;

        }
        
        
        
    }
}

-(instancetype)initWithUnevenHeightStrings:(NSArray *)strings stringFonts:(NSArray *)fonts originY:(NSArray *)originY stringColors:(NSArray *)colors {
    self=[super init];
    self.strings=strings;
    self.fonts=fonts;
    self.originY=originY;
    self.fontColors=colors;
    //[self setNeedsDisplay];
    return self;
}

@end
复制代码

 

 

Demo:

 

使用方法很简单,直接在需要使用的地方调用,如下:

 

复制代码
 UnevenHeightLabel  *label=[[UnevenHeightLabel alloc] initWithUnevenHeightStrings:@[@"11.",@"00",@"%"]  stringFonts:@[[UIFont systemFontOfSize:20],[UIFont systemFontOfSize:25],[UIFont systemFontOfSize:30]] originY:@[@0,@0,@0] stringColors:@[[UIColor redColor],[UIColor blueColor],[UIColor greenColor]]];
    
    label.frame=CGRectMake(100, 100, 200, 30);
    label.backgroundColor=[UIColor clearColor];
    [self.view addSubview:label];
    UnevenHeightLabel *mylabel=[[UnevenHeightLabel alloc] initWithUnevenHeightStrings:@[@"A",@"a",@"B",@"b",@"C",@"c"]
                                                                          stringFonts:@[[UIFont systemFontOfSize:25],
                                                                                        [UIFont systemFontOfSize:20],
                                                                                        [UIFont systemFontOfSize:25],
                                                                                        [UIFont systemFontOfSize:20],
                                                                                        [UIFont systemFontOfSize:25],
                                                                                        [UIFont systemFontOfSize:20]]
                                                                              originY:@[@0,@0,@0,@0,@0,@0]
                                                                         stringColors:@[
                                                                                        [UIColor redColor],
                                                                                        [UIColor orangeColor],
                                                                                        [UIColor greenColor],
                                                                                        [UIColor blueColor],
                                                                                        [UIColor cyanColor],
                                                                                        [UIColor purpleColor]]];
    [mylabel setFrame:CGRectMake(SCREEN_WIDTH/2-55, SCREEN_HEIGHT/2-30, 110, 50)];
    [mylabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:mylabel];
复制代码

 

 

  

效果如下:

 

 

posted @   msp的昌伟哥哥  阅读(1215)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示