CoreText 框架中最經常使用的幾個類:html
- CTFont
- CTFontCollection
- CTFontDescriptor
- CTFrame
- CTFramesetter
- CTGlyphInfo
- CTLine
- CTParagraphStyle
- CTRun
- CTTextTab
- CTTypesetter
先來了解一下該框架的總體視窗組合圖:app
![](http://static.javashuo.com/static/loading.gif)
CTFrame 做爲一個總體的畫布(Canvas),其中由行(CTLine)組成,而每行能夠分爲一個或多個小方塊(CTRun)。框架
注意:你不須要本身建立CTRun,Core Text將根據NSAttributedString的屬性來自動建立CTRun。每一個CTRun對象對應不一樣的屬性,正所以,你能夠自由的控制字體、顏色、字間距等等信息。oop
一般處理步聚:字體
1.使用core text就是先有一個要顯示的string,而後定義這個string每一個部分的樣式->attributedString -> 生成 CTFramesetter -> 獲得CTFrame -> 繪製(CTFrameDraw)
其中能夠更詳細的設置換行方式,對齊方式,繪製區域的大小等。
2.繪製只是顯示,點擊事件就須要一個判斷了。
CTFrame 包含了多個CTLine,而且能夠獲得各個line的其實位置與大小。判斷點擊處在不在某個line上。CTLine 又能夠判斷這個點(相對於ctline的座標)處的文字範圍。而後遍歷這個string的全部NSTextCheckingResult,根據result的rang判斷點擊處在不在這個rang上,從而獲得點擊的連接與位置。ui
字體的基本知識:spa
字體(Font):是一系列字號、樣式和磅值相同的字符(例如:10磅黑體Palatino)。現多被視爲字樣的同義詞.net
字面(Face):是全部字號的磅值和格式的綜合code
字體集(Font family):是一組相關字體(例如:Franklin family包括Franklin Gothic、Fran-klinHeavy和Franklin Compressed)orm
磅值(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。因爲字些字母形狀的緣由常常被連寫,故排字員已習慣將它們連寫。
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
字符屬性名稱:
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對象
舉例說明:
- NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:@"This is a test of characterAttribute. 中文字符"];
![](http://static.javashuo.com/static/loading.gif)
- CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);
- [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
- 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)];
- long number = 10;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
- [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];
![](http://static.javashuo.com/static/loading.gif)
- 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)];
![](http://static.javashuo.com/static/loading.gif)
- 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])];
![](http://static.javashuo.com/static/loading.gif)
- 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])];
![](http://static.javashuo.com/static/loading.gif)
在設置空心字顏色時,必須先將字體高爲空心,不然設置顏色是沒有效果的。
-
- 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)];
![](http://static.javashuo.com/static/loading.gif)
最後是draw了。
- -(void)characterAttribute
- {
- NSString *str = @"This is a test of characterAttribute. 中文字符";
- NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:str];
-
- [mabstring beginEditing];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
-
-
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- CGContextSetTextMatrix(context , CGAffineTransformIdentity);
-
-
-
- CGContextSaveGState(context);
-
-
- CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
-
-
- CGContextScaleCTM(context, 1.0 ,-1.0);
-
- CTFrameDraw(frame,context);
-
- CGPathRelease(Path);
- CFRelease(framesetter);
- }
- - (void)drawRect:(CGRect)rect
- {
- [self characterAttribute];
- }
CORETEXT框架圖
![](http://static.javashuo.com/static/loading.gif)
另對於Context的瞭解能夠參考:http://www.padovo.com/blog/2013/01/31/study-coretext/