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

来源:互联网 发布:达内软件培训费用 编辑:程序博客网 时间:2024/06/02 06:32

【提示:最好的方法在最后哦!O(∩_∩)O~】

首先导入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];

效果图如下:




补充:另一种方式,利用NSMutableAttributedString,很简便

//iOS6以后 在UILabel显示不同的字体和颜色NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0] range:NSMakeRange(6, 12)];[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];self.attrLabel.attributedText = str;



0 1
原创粉丝点击