API接口文檔韩容。
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html
CoreText 框架中最常用的幾個類:
CTFont
CTFontCollection
CTFontDescriptor
CTFrame
CTFramesetter
CTGlyphInfo
CTLine
CTParagraphStyle
CTRun
CTTextTab
CTTypesetter
字體的基本知識:
字體(Font):是一系列字號、樣式和磅值相同的字符(例如:10磅黑體Palatino)∽荩現(xiàn)多被視為字樣的同義詞
字面(Face):是所有字號的磅值和格式的綜合
字體集(Font family):是一組相關(guān)字體(例如:Franklin family包括Franklin Gothic净嘀、Fran-klinHeavy和Franklin Compressed)
磅值(Weight):用于描述字體粗度狂魔。典型的磅值,從最粗到最細(xì),有極細(xì)、細(xì)凌彬、book沸柔、中等、半粗铲敛、粗褐澎、較粗、極粗
樣式(Style):字形有三種形式:Roman type是直體;oblique type是斜體;utakuc type是斜體兼曲線(比Roman type更像書法體)伐蒋。
x高度(X height):指小寫字母的平均高度(以x為基準(zhǔn))工三。磅值相同的兩字母,x高度越大的字母看起來比x高度小的字母要大
Cap高度(Cap height):與x高度相似。指大寫字母的平均高度(以C為基準(zhǔn))
下行字母(Descender):例如在字母q中,基線以下的字母部分叫下伸部分
上行字母(Ascender):x高度以上的部分(比如字母b)叫做上伸部分
基線(Baseline):通常在x先鱼、v徒蟆、b、m下的那條線
描邊(Stroke):組成字符的線或曲線型型《紊螅可以加粗或改變字符形狀
襯線(Serif):用來使字符更可視的一條水平線。如字母左上角和下部的水平線闹蒜。
無襯線(Sans Serif):可以讓排字員不使用襯線裝飾寺枉。
方形字(Block):這種字體的筆畫使字符看起來比無襯線字更顯眼,但還不到常見的襯線字的程度。例如Lubalin Graph就是方形字,這種字看起來好像是木頭塊刻的一樣
手寫體腳本(Calligraphic script):是一種仿效手寫體的字體绷落。例如Murray Hill或者Fraktur字體
藝術(shù)字(Decorative):像繪畫般的字體
Pi符號(Pisymbol):非標(biāo)準(zhǔn)的字母數(shù)字字符的特殊符號姥闪。例如Wingdings和Mathematical Pi
連寫(Ligature):是一系列連寫字母如fi、fl砌烁、ffi或ffl筐喳。由于字些字母形狀的原因經(jīng)常被連寫,故排字員已習(xí)慣將它們連寫催式。
//字體形狀屬性? 必須是CFNumberRef對象默認(rèn)為0,非0則對應(yīng)相應(yīng)的字符形狀定義避归,如1表示傳統(tǒng)字符形狀
const CFStringRef kCTFontAttributeName;
//字體屬性? 必須是CTFont對象
const CFStringRef kCTKernAttributeName;
//字符間隔屬性 必須是CFNumberRef對象
const CFStringRef kCTLigatureAttributeName;
//設(shè)置是否使用連字屬性荣月,設(shè)置為0,表示不使用連字屬性梳毙。標(biāo)準(zhǔn)的英文連字有FI,FL.默認(rèn)值為1哺窄,既是使用標(biāo)準(zhǔn)連字。也就是當(dāng)搜索到f時候账锹,會把fl當(dāng)成一個文字萌业。必須是CFNumberRef 默認(rèn)為1,可取0,1,2
const CFStringRef kCTForegroundColorAttributeName;
//字體顏色屬性? 必須是CGColor對象,默認(rèn)為black
const CFStringRef kCTForegroundColorFromContextAttributeName;
//上下文的字體顏色屬性 必須為CFBooleanRef 默認(rèn)為False,
const CFStringRef kCTParagraphStyleAttributeName;
//段落樣式屬性 必須是CTParagraphStyle對象 默認(rèn)為NIL
const CFStringRef kCTStrokeWidthAttributeName;
//筆畫線條寬度 必須是CFNumberRef對象奸柬,默為0.0f生年,標(biāo)準(zhǔn)為3.0f
const CFStringRef kCTStrokeColorAttributeName;
//筆畫的顏色屬性 必須是CGColorRef 對象,默認(rèn)為前景色
const CFStringRef kCTSuperscriptAttributeName;
//設(shè)置字體的上下標(biāo)屬性 必須是CFNumberRef對象 默認(rèn)為0,可為-1為下標(biāo),1為上標(biāo)廓奕,需要字體支持才行晶框。如排列組合的樣式Cn1
const CFStringRef kCTUnderlineColorAttributeName;
//字體下劃線顏色屬性 必須是CGColorRef對象,默認(rèn)為前景色
const CFStringRef kCTUnderlineStyleAttributeName;
//字體下劃線樣式屬性 必須是CFNumberRef對象,默為kCTUnderlineStyleNone 可以通過CTUnderlineStypleModifiers 進(jìn)行修改下劃線風(fēng)格
const CFStringRef kCTVerticalFormsAttributeName;
//文字的字形方向?qū)傩?必須是CFBooleanRef 默認(rèn)為false懂从,false表示水平方向授段,true表示豎直方向
const CFStringRef kCTGlyphInfoAttributeName;
//字體信息屬性 必須是CTGlyphInfo對象
const CFStringRef kCTRunDelegateAttributeName
//CTRun 委托屬性 必須是CTRunDelegate對象
使用案例:
-(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)];
*/
/*
//設(shè)置字體屬性
CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);
[mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
*/
/*
//設(shè)置字體簡隔 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])];
*/
/*
//設(shè)置字體顏色
[mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];
*/
/*
//設(shè)置字體顏色為前影色
CFBooleanRef flag = kCFBooleanTrue;
[mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];
*/
/*
//設(shè)置空心字
long number = 2;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
//設(shè)置空心字顏色
[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)];
*/
/*
//設(shè)置斜體字
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)];
*/
//對同一段字體進(jìn)行多屬性設(shè)置
//紅色
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);
//獲取當(dāng)前(View)上下文以便于之后的繪畫,這個是一個離屏番甩。
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context , CGAffineTransformIdentity);
//壓棧侵贵,壓入圖形狀態(tài)棧中.每個圖形上下文維護(hù)一個圖形狀態(tài)棧,并不是所有的當(dāng)前繪畫環(huán)境的圖形狀態(tài)的元素都被保存缘薛。圖形狀態(tài)中不考慮當(dāng)前路徑窍育,所以不保存
//保存現(xiàn)在得上下文圖形狀態(tài)。不管后續(xù)對context上繪制什么都不會影響真正得屏幕宴胧。
CGContextSaveGState(context);
//x漱抓,y軸方向移動
CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
//縮放x,y軸方向縮放恕齐,-1.0為反向1.0倍,坐標(biāo)系轉(zhuǎn)換,沿x軸翻轉(zhuǎn)180度
CGContextScaleCTM(context, 1.0 ,-1.0);
CTFrameDraw(frame,context);
CGPathRelease(Path);
CFRelease(framesetter);
}
- (void)drawRect:(CGRect)rect
{
[self characterAttribute];
}