1. 程式人生 > >UILabel上展示不同顏色的文字(NSAttributedString)

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

【提示:最好的方法在最後哦!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;