UILabel上展示不同颜色的文字(NSAttributedString)

首先导入CoreText.framework,并在需要使用的文件中导入:  
#import<CoreText/CoreText.h>  
新建一个类,继承UILabel,以下为文件内容:  

MyLabel.h  

//MyLabel.h
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>

@interface MyLabel : UILabel

@end

MyLabel.m

//MyLabel.m
#import "MyLabel.h"

@implementation MyLabel

//NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。
-(void)drawRect:(CGRect)rect{

    [super drawRect:rect];
    
    NSAttributedString *attriString = [self getAttributedString];
    
    //在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标系统不同
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
    
    //CTFramesetter是CTFrame的创建工厂,NSAttributedString需要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上。
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, rect);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    CFRelease(path);
    CFRelease(framesetter);
    
    CTFrameDraw(frame, ctx);
    CFRelease(frame);

    /*
  //------------------------------------------------------------------------
  //----------------取消注释,同样可以实现UILabel上展示不同样式的文字--------------
  //------------------------------------------------------------------------
  CATextLayer *textLayer = [CATextLayer layer];
  textLayer.string = [self getAttributedString];
  textLayer.frame = CGRectMake(0, 50, 200, 200);//可调整位置
  textLayer.backgroundColor = [UIColor purpleColor].CGColor;
  [self.layer addSublayer:textLayer];
  */
}

-(NSMutableAttributedString *)getAttributedString{
    //创建一个NSMutableAttributedString
    NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"Come on,baby!Come on,baby!Come on,baby!"]autorelease];
    //把this的字体颜色变为红色
    [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
            value:(id)[UIColor redColor].CGColor
            range:NSMakeRange(0, 4)];
    //把is变为黄色
    [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
            value:(id)[UIColor yellowColor].CGColor
            range:NSMakeRange(5, 16)];
    //改变this的字体,value必须是一个CTFontRef
    [attriString addAttribute:(NSString *)kCTFontAttributeName
            value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,14,NULL)
            range:NSMakeRange(0, 4)];
    //给this加上下划线,value可以在指定的枚举中选择
    [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
            value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]
            range:NSMakeRange(0, 4)];
    
    /*
  换行的实现
  
  如果想要计算NSAttributedString所要的size,就需要用到这个API:
  CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,因为在CoreText里,行间距也是你来控制的。
  设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子
  属性,其中就包括
  kCTLineBreakByCharWrapping
  kCTParagraphStyleSpecifierLineSpacingAdjustment
  设置如下:
    */
    
    
    /*
  //-------------取消注释,实现换行-------------
  
  CTParagraphStyleSetting lineBreakMode;
  CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping; //换行模式
  lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;
  lineBreakMode.value = &lineBreak;
  lineBreakMode.valueSize = sizeof(CTLineBreakMode);
  //行间距
  CTParagraphStyleSetting LineSpacing;
  CGFloat spacing = 4.0;  //指定间距
  LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
  LineSpacing.value = &spacing;
  LineSpacing.valueSize = sizeof(CGFloat);
  
  CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing};
  CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2);   //第二个参数为settings的长度
  [attriString addAttribute:(NSString *)kCTParagraphStyleAttributeName
  value:(id)paragraphStyle
  range:NSMakeRange(0, attriString.length)];
  */
    
    return attriString;
}

@end

测试代码

先要 #import "MyView.h",在适当位置创建MyLabel的对象,并添加到View中

MyLabel *myLabel = [[MyLabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:myLabel];
[myLabel release];

效果图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值