单一label多颜色多字体

郎言
2023-12-01


核心算法来源:http://code4app.com/ios/单一label多颜色多字体/512439bf6803fa4124000000


调用方法如下

AttributedLabel *label = [[AttributedLabel alloc] initWithFrame:CGRectMake(20, 20, 150, 40)];
    
    
    // 注意!!一定要先给text赋值,然后再加属性;
    label.text = @"this is test ";
    
    [self.view addSubview:label];
    
    // 设置this为红色
    [label setColor:[UIColor redColor] fromIndex:0 length:4];
    
    // 设置is为黄色
    [label setColor:[UIColor yellowColor] fromIndex:5 length:2];
    
    // 设置this字体为加粗16号字
    [label setFont:[UIFont boldSystemFontOfSize:30] fromIndex:0 length:4];
    
    // 给this加上下划线
    [label setStyle:kCTUnderlineStyleDouble fromIndex:0 length:4];
    
    label.backgroundColor = [UIColor clearColor];
    
    
    
    
//    预计到账时间:申请通过后3个工作日内
    AttributedLabel *label2 = [[AttributedLabel alloc] initWithFrame:CGRectMake(20, 120, 250, 100)];
    label2.text = @"预计到账时间:申请通过后3个工作日内";
    [self.view addSubview:label2];
    
    [label2 setColor:[UIColor blueColor] fromIndex:12 length:1];
    
    label2.backgroundColor = [UIColor clearColor];


核心算法:

#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>

@interface AttributedLabel : UILabel{
    NSMutableAttributedString          *_attString;
}


// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length;

// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length;

// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length;

@end

#import "AttributedLabel.h"

@interface AttributedLabel(){

}
@property (nonatomic,retain)NSMutableAttributedString          *attString;
@end

@implementation AttributedLabel
@synthesize attString = _attString;

- (void)dealloc{
//    [_attString release];
//    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.string = _attString;
    textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.layer addSublayer:textLayer];
}

- (void)setText:(NSString *)text{
    [super setText:text];
    if (text == nil) {
        self.attString = nil;
    }else{
        self.attString = [[NSMutableAttributedString alloc] initWithString:text];
    }
}

// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
                        value:(id)color.CGColor
                        range:NSMakeRange(location, length)];
}

// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTFontAttributeName
                        value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
                                                       font.pointSize,
                                                       NULL))
                        range:NSMakeRange(location, length)];
}

// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                        value:(id)[NSNumber numberWithInt:style]
                        range:NSMakeRange(location, length)];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end






 类似资料: