美文网首页
UILabel(自定义)

UILabel(自定义)

作者: Nidalee丶 | 来源:发表于2015-12-26 10:45 被阅读54次

    自定义的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];
    
    

    相关文章

      网友评论

          本文标题:UILabel(自定义)

          本文链接:https://www.haomeiwen.com/subject/vncbhttx.html