UILabel上展示不同颜色的文字(NSAttributedString)
生活随笔
收集整理的這篇文章主要介紹了
UILabel上展示不同颜色的文字(NSAttributedString)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?CSDN博客原文??http://blog.csdn.net/u011439689/article/details/22693679
首先導(dǎo)入CoreText.framework,并在需要使用的文件中導(dǎo)入:??
#import<CoreText/CoreText.h>??
新建一個類,繼承UILabel,以下為文件內(nèi)容:??
MyLabel.h??
//MyLabel.h
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>@interface MyLabel : UILabel@end
#import<CoreText/CoreText.h>??
新建一個類,繼承UILabel,以下為文件內(nèi)容:??
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];//在代碼中我們調(diào)整了CTM(current transformation matrix),這是因為Quartz 2D的坐標(biāo)系統(tǒng)不同CGContextRef ctx = UIGraphicsGetCurrentContext();CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));//CTFramesetter是CTFrame的創(chuàng)建工廠,NSAttributedString需要通過CTFrame繪制到界面上,得到CTFramesetter后,創(chuàng)建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);/* //------------------------------------------------------------------------ //----------------取消注釋,同樣可以實現(xiàn)UILabel上展示不同樣式的文字-------------- //------------------------------------------------------------------------ CATextLayer *textLayer = [CATextLayer layer]; textLayer.string = [self getAttributedString]; textLayer.frame = CGRectMake(0, 50, 200, 200);//可調(diào)整位置 textLayer.backgroundColor = [UIColor purpleColor].CGColor; [self.layer addSublayer:textLayer]; */ }-(NSMutableAttributedString *)getAttributedString{//創(chuàng)建一個NSMutableAttributedStringNSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"Come on,baby!Come on,baby!Come on,baby!"]autorelease];//把this的字體顏色變?yōu)榧t色[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];//把is變?yōu)辄S色[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)];/* 換行的實現(xiàn) 如果想要計算NSAttributedString所要的size,就需要用到這個API: CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行時會算不準的,因為在CoreText里,行間距也是你來控制的。 設(shè)置行間距和換行模式都是設(shè)置一個屬性:kCTParagraphStyleAttributeName,這個屬性里面又分為很多子 屬性,其中就包括 kCTLineBreakByCharWrapping kCTParagraphStyleSpecifierLineSpacingAdjustment 設(shè)置如下:*//* //-------------取消注釋,實現(xiàn)換行------------- 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); //第二個參數(shù)為settings的長度 [attriString addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, attriString.length)]; */return attriString; }@end測試代碼
先要 #import "MyView.h",在適當(dāng)位置創(chuàng)建MyLabel的對象,并添加到View中
MyLabel *myLabel = [[MyLabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:myLabel]; [myLabel release];效果圖如下:
總結(jié)
以上是生活随笔為你收集整理的UILabel上展示不同颜色的文字(NSAttributedString)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AutoLayout代码布局使用大全—一
- 下一篇: AutoLayout ScrollVie