UILabel(自定义)

自定义的label实现

-(void)textArr:(NSArray *)textArr colorArr:(NSArray *)colorArr fontArr:(NSArray *)fontArr {
    
    //控制组数(保护)
    NSInteger letterCount = 0;
    
    letterCount = textArr.count < colorArr.count ? textArr.count : colorArr.count;
    letterCount = letterCount < fontArr.count ? letterCount : fontArr.count;
    
    //拼接字符串
    NSMutableString *text = [NSMutableString string];
    
    for (NSString *str in textArr) {
        
        [text appendString:str];
        
    }
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
    
    //赋值属性
    NSInteger startLoc = 0;
    
    for (int i = 0; i < letterCount; i++) {
        
        [str addAttributes:@{NSForegroundColorAttributeName:colorArr[i], NSFontAttributeName:[UIFont systemFontOfSize:[fontArr[i] integerValue]]} range:NSMakeRange(startLoc, [textArr[i] length])];
        startLoc += [textArr[i] length];
    }
    self.attributedText = str;
}

部分有局限,待整理

NSArray *a = [NSArray arrayWithObjects:@"费尔南多托雷斯/", @"格列兹曼/", @"比利亚", nil];
    
    NSArray *b = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blackColor], [UIColor blueColor], nil];
    NSArray *c = [NSArray arrayWithObjects:@"19", @"13", @"30", nil];
    
    abcLabel *label = [[abcLabel alloc] init];
    label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label];
    label.frame = CGRectMake(0, 0, 375, 50);
    label.center = self.view.center;
    [label textArr:a colorArr:b fontArr:c];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容