NSAttributedString可以讓我們使一個字符串顯示的多樣化,但是目前到iOS 5爲止,好像對它支持的不是很好,因爲顯示起來不太方便(至少沒有在OS X上方便)。
首先導入CoreText.framework,並在需要使用的文件中導入:
#import<CoreText/CoreText.h>
創建一個NSMutableAttributedString:
- NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]
- 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, 2)];
- //改變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)];
- return attriString;
這樣就算是配置好了,但是我們可以發現NSAttributedString繼承於NSObject,並且不支持任何draw的方法,那我們就只能自己draw了。寫一個UIView的子類(假設命名爲TView),在initWithFrame中把背景色設爲透明(self.backgroundColor = [UIColor clearColor]),然後在重寫drawRect方法:
- -(void)drawRect:(CGRect)rect{
- [super drawRect:rect];
- NSAttributedString *attriString = getAttributedString();
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
- 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);
- }
在代碼中我們調整了CTM(current transformation matrix),這是因爲Quartz 2D的座標系統不同,比如(10, 10)到(20, 20)的直線座標:
座標類似於數學中的座標,可以先不調整CTM,看它是什麼樣子的,下面兩種調整方法是完全一樣的:
- CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
- CGContextTranslateCTM(ctx, 0, rect.size.height);
- CGContextScaleCTM(ctx, 1, -1);
CTFramesetter是CTFrame的創建工廠,NSAttributedString需要通過CTFrame繪製到界面上,得到CTFramesetter後,創建path(繪製路徑),然後得到CTFrame,最後通過CTFrameDraw方法繪製到界面上。
如果想要計算NSAttributedString所要的size,就需要用到這個API:
CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行時會算不準的,因爲在CoreText裏,行間距也是你來控制的。
設置行間距和換行模式都是設置一個屬性:kCTParagraphStyleAttributeName,這個屬性裏面又分爲很多子
屬性,其中就包括
- kCTLineBreakByCharWrapping
- kCTParagraphStyleSpecifierLineSpacingAdjustment
- //段落
- //line break
- 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的長度
- [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName
- value:(id)paragraphStyle
- range:NSMakeRange(0, attributedString.length)];
-----------------------------------------猥瑣的分界線-----------------------------------------
這並不是唯一的方法,還有另一種替代方案:
- CATextLayer *textLayer = [CATextLayer layer];
- textLayer.string = getAttributedString();
- textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200);
- [self.view.layer addSublayer:textLayer];
-----------------------------------------猥瑣的分界線-----------------------------------------
效果圖:
源碼地址
原文中確有描述不適當的地方,比如:The upper-left corner of the context is (0, 0) 。實際上Quartz2D的座標系統確實在左下角,只是有一些技術在設置它們的graphics context時使用了不同於Quartz的默認座標系統。相對於Quartz來說,這些座標系統是修改的座標系統(modified coordinate system)。
UPDATED
在iOS6之後,創建一個AttributedString變成了一件輕鬆的事情,<CoreText/CoreText.h>已經不需要導入了。如果我要設置字體的顏色,可以直接這樣:[textAttr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, text.length)];
如果要計算一個NSAttributedString的size,使用NSAttributedString的這個API:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);
但是需要注意一點,如果調用這個API的NSAttributedString不包含字體、行高等有利於計算的數據,那最終計算出來的size可能和實際有所出入。
原文轉之:http://blog.csdn.net/zhangao0086/article/details/7616385