IOS CoreText.framevork 的基本用法

API接口文檔。html

https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_Framework_Ref/_index.htmlapp


CoreText 框架中最經常使用的幾個類:框架

  1. CTFont字體

  2. CTFontCollectionui

  3. CTFontDescriptorspa

  4. CTFrame.net

  5. CTFramesettercode

  6. CTGlyphInfoorm

  7. CTLinehtm

  8. CTParagraphStyle

  9. CTRun

  10. CTTextTab

  11. 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,2
const CFStringRef kCTForegroundColorAttributeName;

//字體顏色屬性  必須是CGColor對象,默認爲black
const CFStringRef kCTForegroundColorFromContextAttributeName;

 //上下文的字體顏色屬性 必須爲CFBooleanRef 默認爲False,
const CFStringRef kCTParagraphStyleAttributeName;

//段落樣式屬性 必須是CTParagraphStyle對象 默認爲NIL
const CFStringRef kCTStrokeWidthAttributeName;

//筆畫線條寬度 必須是CFNumberRef對象,默爲0.0f,標準爲3.0f
const CFStringRef kCTStrokeColorAttributeName;

//筆畫的顏色屬性 必須是CGColorRef 對象,默認爲前景色
const CFStringRef kCTSuperscriptAttributeName;

//設置字體的上下標屬性 必須是CFNumberRef對象 默認爲0,可爲-1爲下標,1爲上標,須要字體支持才行。如排列組合的樣式Cn1
const 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

  1. NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:@"This is a test of characterAttribute. 中文字符"];  



[cpp] view plaincopy

  1. //設置字體屬性  

  2.   CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);  

  3.   [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];   


[cpp] view plaincopy

  1. //設置斜體字  

  2.     CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);  

  3.     [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];  


[cpp] view plaincopy

  1. //下劃線  

  2.     [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];   


[cpp] view plaincopy

  1. //下劃線顏色  

  2.     [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];  



[cpp] view plaincopy

  1. //設置字體簡隔 eg:test   

  2.     long number = 10;  

  3.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);  

  4.     [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];  


[cpp] view plaincopy

  1. //設置連字  

  2. long number = 1;  

  3.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);  

  4.     [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])];  

連字還不會使用,未看到效果。

[cpp] view plaincopy

  1. //設置字體顏色  

  2.     [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];  


[cpp] view plaincopy

  1. //設置字體顏色爲前影色  

  2.     CFBooleanRef flag = kCFBooleanTrue;  

  3.     [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];  

無明顯效果。

[cpp] view plaincopy

  1. //設置空心字  

  2.     long number = 2;  

  3.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);  

  4.     [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];  


[cpp] view plaincopy

  1. //設置空心字  

  2.     long number = 2;  

  3.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);  

  4.     [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];  

  5.        

  6.     //設置空心字顏色  

  7.     [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];  


在設置空心字顏色時,必須先將字體高爲空心,不然設置顏色是沒有效果的。

[cpp] view plaincopy

  1. //對同一段字體進行多屬性設置      

  2.     //紅色  

  3.     NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];  

  4.     //斜體  

  5.     CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);  

  6.     [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];  

  7.     //下劃線  

  8.     [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];  

  9.       

  10.     [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];  


最後是draw了。

[cpp] view plaincopy

  1. -(void)characterAttribute  

  2. {  

  3.     NSString *str = @"This is a test of characterAttribute. 中文字符";  

  4.     NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:str];  

  5.       

  6.     [mabstring beginEditing];  

  7.     /* 

  8.     long number = 1; 

  9.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); 

  10.     [mabstring addAttribute:(id)kCTCharacterShapeAttributeName value:(id)num range:NSMakeRange(0, 4)]; 

  11.     */  

  12.     /* 

  13.     //設置字體屬性 

  14.     CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL); 

  15.     [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)]; 

  16.     */  

  17.     /* 

  18.     //設置字體簡隔 eg:test  

  19.     long number = 10; 

  20.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); 

  21.     [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)]; 

  22.     */  

  23.   

  24.     /* 

  25.     long number = 1; 

  26.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); 

  27.     [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])]; 

  28.      */  

  29.     /* 

  30.     //設置字體顏色 

  31.     [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)]; 

  32.      */  

  33.     /* 

  34.     //設置字體顏色爲前影色 

  35.     CFBooleanRef flag = kCFBooleanTrue; 

  36.     [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)]; 

  37.      */  

  38.       

  39.     /* 

  40.     //設置空心字 

  41.     long number = 2; 

  42.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); 

  43.     [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])]; 

  44.       

  45.     //設置空心字顏色 

  46.     [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])]; 

  47.      */  

  48.       

  49.     /* 

  50.     long number = 1; 

  51.     CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number); 

  52.     [mabstring addAttribute:(id)kCTSuperscriptAttributeName value:(id)num range:NSMakeRange(3, 1)]; 

  53.     */  

  54.       

  55.     /* 

  56.     //設置斜體字 

  57.     CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL); 

  58.     [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)]; 

  59.     */   

  60.       

  61.     /* 

  62.     //下劃線 

  63.     [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];  

  64.     //下劃線顏色 

  65.     [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)]; 

  66.      */  

  67.       

  68.       

  69.       

  70.     //對同一段字體進行多屬性設置      

  71.     //紅色  

  72.     NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];  

  73.     //斜體  

  74.     CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);  

  75.     [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];  

  76.     //下劃線  

  77.     [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];  

  78.       

  79.     [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];  

  80.        

  81.   

  82.       

  83.     NSRange kk = NSMakeRange(0, 4);  

  84.       

  85.     NSDictionary * dc = [mabstring attributesAtIndex:0 effectiveRange:&kk];  

  86.       

  87.     [mabstring endEditing];  

  88.       

  89.     NSLog(@"value = %@",dc);  

  90.       

  91.   

  92.       

  93.     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);  

  94.       

  95.     CGMutablePathRef Path = CGPathCreateMutable();  

  96.       

  97.     CGPathAddRect(Path, NULL ,CGRectMake(10 , 0 ,self.bounds.size.width-10 , self.bounds.size.height-10));  

  98.       

  99.     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);      

  100.       

  101.     //獲取當前(View)上下文以便於以後的繪畫,這個是一個離屏。  

  102.     CGContextRef context = UIGraphicsGetCurrentContext();  

  103.       

  104.     CGContextSetTextMatrix(context , CGAffineTransformIdentity);  

  105.       

  106.     //壓棧,壓入圖形狀態棧中.每一個圖形上下文維護一個圖形狀態棧,並非全部的當前繪畫環境的圖形狀態的元素都被保存。圖形狀態中不考慮當前路徑,因此不保存  

  107.     //保存如今得上下文圖形狀態。無論後續對context上繪製什麼都不會影響真正得屏幕。  

  108.     CGContextSaveGState(context);  

  109.       

  110.     //x,y軸方向移動  

  111.     CGContextTranslateCTM(context , 0 ,self.bounds.size.height);  

  112.       

  113.     //縮放x,y軸方向縮放,-1.0爲反向1.0倍,座標系轉換,沿x軸翻轉180度  

  114.     CGContextScaleCTM(context, 1.0 ,-1.0);  

  115.       

  116.     CTFrameDraw(frame,context);  

  117.       

  118.     CGPathRelease(Path);  

  119.     CFRelease(framesetter);  

  120. }  


[cpp] view plaincopy

  1. - (void)drawRect:(CGRect)rect  

  2. {  

  3.     [self characterAttribute];  

  4. }  



CORETEXT框架圖


另對於Context的瞭解能夠參考:http://www.padovo.com/blog/2013/01/31/study-coretext/

相關文章
相關標籤/搜索