自定义Label,设置关键字的颜色

上官景铄
2023-12-01

下面是一个自定义UILabel ,通过他,可以给Label中的关键字设置颜色

#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
@interface CustomLabel : UILabel
{
    NSMutableAttributedString   *_mattString;
}
//针对某节文字设置单一颜色
-(void)setSubTextColorArray:(NSArray*)colorArray subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray;

//针对某节文字设置单一颜色
-(void)setSubTextColor:(UIColor*)color subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray;

// 设置某段字的颜色
- (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 "CustomLabel.h"

@implementation CustomLabel
- (void)dealloc
{
    [_mattString release];
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)drawRect:(CGRect)rect{
    
    if (self.tag==10) {
          [super drawRect:rect];
    }
    else{
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.string = _mattString;
    textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    NSArray* arr=[NSArray arrayWithObject:textLayer];
    [self.layer setSublayers:arr];
    }

}

- (void)setText:(NSString *)text{
    [super setText:text];
    if (text == nil) {
        [_mattString release];
        _mattString = nil;
    }else{
        _mattString = [[[[NSMutableAttributedString alloc] initWithString:text]retain] autorelease];
    }
}
//针对某节文字设置单一颜色
-(void)setSubTextColorArray:(NSArray*)colorArray subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray
{
    if ([colorArray count]==[subTextPositionRangeArray count]) {
        for (int i=0; i<[colorArray count]; i++) {
            NSRange tempRange=((NSValue*)[subTextPositionRangeArray objectAtIndex:i]).rangeValue;
            [self setColor:[colorArray objectAtIndex:i] fromIndex:tempRange.location length:tempRange.length];
        }
    }
    else
    {
        NSLog(@"ERROR:colorArray count!=subTextPositionRangeArray count");
    }
}


//针对某节文字设置单一颜色
-(void)setSubTextColor:(UIColor*)color subTextPositionRangeArray:(NSArray*)subTextPositionRangeArray
{
    for (NSValue* tempVale in subTextPositionRangeArray) {
        NSRange tempRange=tempVale.rangeValue;
        [self setColor:color fromIndex:tempRange.location length:tempRange.length];
    }
}
// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_mattString 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;
    }
    [_mattString addAttribute:(NSString *)kCTFontAttributeName
                       value:(id)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;
    }
    [_mattString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                       value:(id)[NSNumber numberWithInt:style]
                       range:NSMakeRange(location, length)];
}




 类似资料: