CTTypesetter
先來瞭解一下該架構的整體視窗組合圖:
CTFrame 作為一個整體的畫布(Canvas),其中由行(CTLine)組成,而每行可以分為一個或多個小方塊(CTRun)。
注意:你不需要自己建立CTRun,Core Text將根據NSAttributedString的屬性來自動建立CTRun。每個CTRun對象對應不同的屬性,正因此,你可以自由的控制字型、顏色、字間距等等資訊。
通常處理步聚:
1.使用core text就是先有一個要顯示的string,然後定義這個string每個部分的樣式->attributedString -> 產生 CTFramesetter -> 得到CTFrame -> 繪製(CTFrameDraw)
其中可以更詳細的設定換行方式,對齊,繪製地區的大小等。
2.繪製只是顯示,點擊事件就需要一個判斷了。
CTFrame 包含了多個CTLine,並且可以得到各個line的其實位置與大小。判斷點擊處在不在某個line上。CTLine 又可以判斷這個點(相對於ctline的座標)處的文字範圍。然後遍曆這個string的所有NSTextCheckingResult,根據result的rang判斷點擊處在不在這個rang上,從而得到點擊的連結與位置。
字型的基本知識:
字型(Font):是一系列字型大小、樣式和磅值相同的字元(例如:10磅黑體Palatino)。現多被視為字樣的同義字
字面(Face):是所有字型大小的磅值和格式的綜合
字型集(Font family):是一組相關字型(例如:Franklin family包括Franklin Gothic、Fran-klinHeavy和Franklin Compressed)
磅值(Weight):用於描述字型粗度。典型的磅值,從最粗到最細,有極細、細、book、中等、半粗、粗、較粗、極粗
樣式(Style):字形有三種形式:Roman type是直體;oblique type是斜體;utakuc type是斜體兼曲線(比Roman type更像書法體)。
x高度(X height):指小寫字母的平均高度(以x為基準)。磅值相同的兩字母,x高度越大的字母看起來比x高度小的字母要大
Cap高度(Cap height):與x高度相似。指大寫字母的平均高度(以C為基準)
下行字母(Descender):例如在字母q中,基準以下的字母部分叫下伸部分
上行字母(Ascender):x高度以上的部分(比如字母b)叫做上伸部分
基準(Baseline):通常在x、v、b、m下的那條線
描邊(Stroke):組成字元的線或曲線。可以加粗或改變字元形狀
襯線(Serif):用來使字元更可視的一條水平線。如字母左上方和下部的水平線。
無襯線(Sans Serif):可以讓排字員不使用襯線裝飾。
方形字(Block):這種字型的筆畫使字元看起來比無襯線字更顯眼,但還不到常見的襯線字的程度。例如Lubalin Graph就是方形字,這種字看起來好像是木頭塊刻的一樣
手寫體指令碼(Calligraphic script):是一種仿效手寫體的字型。例如Murray Hill或者Fraktur字型
藝術字(Decorative):像繪畫般的字型
Pi符號(Pisymbol):非標準的字母數字字元的特殊符號。例如Wingdings和Mathematical Pi
連寫(Ligature):是一系列連寫字母如fi、fl、ffi或ffl。由於字些字母形狀的原因經常被連寫,故排字員已習慣將它們連寫。
字元屬性名稱:
const CFStringRef kCTCharacterShapeAttributeName;
//字型形狀屬性 必須是CFNumberRef對象預設為0,非0則對應相應的字元形狀定義,如1表示傳統字元形狀
const CFStringRef kCTFontAttributeName;
//字型屬性 必須是CTFont對象const CFStringRef kCTKernAttributeName;
//字元間隔屬性 必須是CFNumberRef對象const CFStringRef kCTLigatureAttributeName;
//設定是否使用連字屬性,設定為0,表示不使用連字屬性。標準的英文連字有FI,FL.預設值為1,既是使用標準連字。也就是當搜尋到f時候,會把fl當成一個文字。必須是CFNumberRef 預設為1,可取0,1,2const CFStringRef kCTForegroundColorAttributeName;
//字型顏色屬性 必須是CGColor對象,預設為blackconst CFStringRef kCTForegroundColorFromContextAttributeName;
//內容相關的字型顏色屬性 必須為CFBooleanRef 預設為False,const CFStringRef kCTParagraphStyleAttributeName;
//段落樣式屬性 必須是CTParagraphStyle對象 預設為NILconst CFStringRef kCTStrokeWidthAttributeName;
//筆畫線條寬度 必須是CFNumberRef對象,默為0.0f,標準為3.0fconst CFStringRef kCTStrokeColorAttributeName;
//筆畫的顏色屬性 必須是CGColorRef 對象,預設為前景色彩const CFStringRef kCTSuperscriptAttributeName;
//設定字型的上下標屬性 必須是CFNumberRef對象 預設為0,可為-1為下標,1為上標,需要字型支援才行。如排列組合的樣式Cn1const CFStringRef kCTUnderlineColorAttributeName;
//字型底線顏色屬性 必須是CGColorRef對象,預設為前景色彩const CFStringRef kCTUnderlineStyleAttributeName;
//字型底線樣式屬性 必須是CFNumberRef對象,默為kCTUnderlineStyleNone 可以通過CTUnderlineStypleModifiers 進行修改底線風格const CFStringRef kCTVerticalFormsAttributeName;
//文字的字形方向屬性 必須是CFBooleanRef 預設為false,false表示水平方向,true表示豎直方向const CFStringRef kCTGlyphInfoAttributeName;
//字型資訊屬性 必須是CTGlyphInfo對象const CFStringRef kCTRunDelegateAttributeName
//CTRun 委託屬性 必須是CTRunDelegate對象
舉例說明:
[cpp] view plaincopy
- NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:@This is a test of characterAttribute. 中文字元];
[cpp] view plaincopy
- //設定字型屬性
- CTFontRef font = CTFontCreateWithName(CFSTR(Georgia), 40, NULL);
- [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
[cpp] view plaincopy
- //設定斜體字
- CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);
- [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
[cpp] view plaincopy
- //底線
- [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];
[cpp] view plaincopy
- //底線顏色
- [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];
[cpp] view plaincopy
- //設定字型簡隔 eg:test
- long number = 10;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];
[cpp] view plaincopy
- //設定連字
- long number = 1;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])]; 連字還不會使用,未看到效果。
[cpp] view plaincopy
- //設定字型顏色
- [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];
[cpp] view plaincopy
- //設定字型顏色為前影色
- CFBooleanRef flag = kCFBooleanTrue;
- [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)]; 無明顯效果。
[cpp] view plaincopy
- //設定空心字
- long number = 2;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
[cpp] view plaincopy
- //設定空心字
- long number = 2;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
-
- //設定空心字顏色
- [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];
在設定空心字顏色時,必須先將字型高為空白心,否則設定顏色是沒有效果的。
[cpp] view plaincopy
- //對同一段字型進行多屬性設定
- //紅色
- NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];
- //斜體
- CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);
- [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];
- //底線
- [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];
-
- [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];
最後是draw了。
[cpp] view plaincopy
- -(void)characterAttribute
- {
- NSString *str = @This is a test of characterAttribute. 中文字元;
- NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:str];
-
- [mabstring beginEditing];
- /*
- long number = 1;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTCharacterShapeAttributeName value:(id)num range:NSMakeRange(0, 4)];
- */
- /*
- //設定字型屬性
- CTFontRef font = CTFontCreateWithName(CFSTR(Georgia), 40, NULL);
- [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
- */
- /*
- //設定字型簡隔 eg:test
- long number = 10;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];
- */
-
- /*
- long number = 1;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])];
- */
- /*
- //設定字型顏色
- [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];
- */
- /*
- //設定字型顏色為前影色
- CFBooleanRef flag = kCFBooleanTrue;
- [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];
- */
-
- /*
- //設定空心字
- long number = 2;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
-
- //設定空心字顏色
- [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];
- */
-
- /*
- long number = 1;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTSuperscriptAttributeName value:(id)num range:NSMakeRange(3, 1)];
- */
-
- /*
- //設定斜體字
- CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);
- [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
- */
-
- /*
- //底線
- [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];
- //底線顏色
- [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];
- */
-
-
-
- //對同一段字型進行多屬性設定
- //紅色
- NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];
- //斜體
- CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);
- [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];
- //底線
- [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];
-
- [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];
-
-
-
- NSRange kk = NSMakeRange(0, 4);
-
- NSDictionary * dc = [mabstring attributesAtIndex:0 effectiveRange:&kk];
-
- [mabstring endEditing];
-
- NSLog(@value = %@,dc);
-
-
-
- CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);
-
- CGMutablePathRef Path = CGPathCreateMutable();
-
- CGPathAddRect(Path, NULL ,CGRectMake(10 , 0 ,self.bounds.size.width-10 , self.bounds.size.height-10));
-
- CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);
-
- //擷取當前(View)上下文以便於之後的繪畫,這個是一個離屏。
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- CGContextSetTextMatrix(context , CGAffineTransformIdentity);
-
- //壓棧,壓入圖形狀態棧中.每個圖形上下文維護一個圖形狀態棧,並不是所有的當前繪畫環境的圖形狀態的元素都被儲存。圖形狀態中不考慮當前路徑,所以不儲存
- //儲存現在得上下文圖形狀態。不管後續對context上繪製什麼都不會影響真正得螢幕。
- CGContextSaveGState(context);
-
- //x,y軸方向移動
- CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
-
- //縮放x,y軸方向縮放,-1.0為反向1.0倍,座標系轉換,沿x軸翻轉180度
- CGContextScaleCTM(context, 1.0 ,-1.0);
-
- CTFrameDraw(frame,context);
-
- CGPathRelease(Path);
- CFRelease(framesetter);
- }
[cpp] view plaincopy
- - (void)drawRect:(CGRect)rect
- {
- [self characterAttribute];
- }
CORETEXT架構圖
另對於Context的瞭解可以參考:http://www.padovo.com/blog/2013/01/31/study-coretext/