日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/9/30 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UILabel上展示不同颜色的文字(NSAttributedString) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
時(shí)間?2014-03-31 21:18:28??CSDN博客原文??http://blog.csdn.net/u011439689/article/details/22693679 首先導(dǎo)入CoreText.framework,并在需要使用的文件中導(dǎo)入:??
#import<CoreText/CoreText.h>??
新建一個(gè)類,繼承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),這是因?yàn)镼uartz 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);/* //------------------------------------------------------------------------ //----------------取消注釋,同樣可以實(shí)現(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)建一個(gè)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必須是一個(gè)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)];/* 換行的實(shí)現(xiàn) 如果想要計(jì)算NSAttributedString所要的size,就需要用到這個(gè)API: CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行時(shí)會(huì)算不準(zhǔn)的,因?yàn)樵贑oreText里,行間距也是你來控制的。 設(shè)置行間距和換行模式都是設(shè)置一個(gè)屬性:kCTParagraphStyleAttributeName,這個(gè)屬性里面又分為很多子 屬性,其中就包括 kCTLineBreakByCharWrapping kCTParagraphStyleSpecifierLineSpacingAdjustment 設(shè)置如下:*//* //-------------取消注釋,實(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); //第二個(gè)參數(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的對(duì)象,并添加到View中

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

效果圖如下:


總結(jié)

以上是生活随笔為你收集整理的UILabel上展示不同颜色的文字(NSAttributedString)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。