iOS開發(fā) Coretext基本用法旺遮, 講得很好毒返。 - 博客頻道
轉(zhuǎn)至?http://blog.csdn.NET/fengsh998/article/details/8691823
API接口文檔劝萤。
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html
CoreText 框架中最常用的幾個類:
先來了解一下該框架的整體視窗組合圖:
CTFrame 作為一個整體的畫布(Canvas)沮协,其中由行(CTLine)組成住册,而每行可以分為一個或多個小方塊(CTRun)牢屋。
注意:你不需要自己創(chuàng)建CTRun,Core Text將根據(jù)NSAttributedString的屬性來自動創(chuàng)建CTRun耙册。每個CTRun對象對應不同的屬性给僵,正因此,你可以自由的控制字體、顏色帝际、字間距等等信息蔓同。
通常處理步聚:
1.使用core text就是先有一個要顯示的string,然后定義這個string每個部分的樣式->attributedString -> 生成 CTFramesetter -> 得到CTFrame -> 繪制(CTFrameDraw)
其中可以更詳細的設(shè)置換行方式蹲诀,對齊方式斑粱,繪制區(qū)域的大小等。
2.繪制只是顯示脯爪,點擊事件就需要一個判斷了则北。
CTFrame 包含了多個CTLine,并且可以得到各個line的其實位置與大小。判斷點擊處在不在某個line上痕慢。CTLine 又可以判斷這個點(相對于ctline的坐標)處的文字范圍尚揣。然后遍歷這個string的所有NSTextCheckingResult,根據(jù)result的rang判斷點擊處在不在這個rang上掖举,從而得到點擊的鏈接與位置快骗。
字體的基本知識:
字體(Font):是一系列字號、樣式和磅值相同的字符(例如:10磅黑體Palatino)∷危現(xiàn)多被視為字樣的同義詞
字面(Face):是所有字號的磅值和格式的綜合
字體集(Font family):是一組相關(guān)字體(例如: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字體
藝術(shù)字(Decorative):像繪畫般的字體
Pi符號(Pisymbol):非標準的字母數(shù)字字符的特殊符號。例如Wingdings和Mathematical Pi
連寫(Ligature):是一系列連寫字母如fi愤诱、fl云头、ffi或ffl。由于字些字母形狀的原因經(jīng)常被連寫,故排字員已習慣將它們連寫淫半。
字符屬性名稱:
const CFStringRef kCTCharacterShapeAttributeName;
//字體形狀屬性? 必須是CFNumberRef對象默認為0溃槐,非0則對應相應的字符形狀定義,如1表示傳統(tǒng)字符形狀
const CFStringRef kCTFontAttributeName;
//字體屬性? 必須是CTFont對象
const CFStringRef kCTKernAttributeName;
//字符間隔屬性 必須是CFNumberRef對象
const CFStringRef kCTLigatureAttributeName;
//設(shè)置是否使用連字屬性科吭,設(shè)置為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;
//設(shè)置字體的上下標屬性 必須是CFNumberRef對象 默認為0,可為-1為下標,1為上標,需要字體支持才行培慌。如排列組合的樣式Cn1
const CFStringRef kCTUnderlineColorAttributeName;
//字體下劃線顏色屬性 必須是CGColorRef對象豁陆,默認為前景色
const CFStringRef kCTUnderlineStyleAttributeName;
//字體下劃線樣式屬性 必須是CFNumberRef對象,默為kCTUnderlineStyleNone 可以通過CTUnderlineStypleModifiers 進行修改下劃線風格
const CFStringRef kCTVerticalFormsAttributeName;
//文字的字形方向?qū)傩?必須是CFBooleanRef 默認為false,false表示水平方向吵护,true表示豎直方向
const CFStringRef kCTGlyphInfoAttributeName;
//字體信息屬性 必須是CTGlyphInfo對象
const CFStringRef kCTRunDelegateAttributeName
//CTRun 委托屬性 必須是CTRunDelegate對象
舉例說明:
NSMutableAttributedString?*mabstring?=?[[NSMutableAttributedString?alloc]initWithString:@"This?is?a?test?of?characterAttribute.?中文字符"];
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)];
longnumber?=?10;
CFNumberRef?num?=?CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring?addAttribute:(id)kCTKernAttributeName?value:(id)num?range:NSMakeRange(10,?4)];
longnumber?=?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)];
無明顯效果馅而。
longnumber?=?2;
CFNumberRef?num?=?CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring?addAttribute:(id)kCTStrokeWidthAttributeName?value:(id)num?range:NSMakeRange(0,?[str?length])];
longnumber?=?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])];
在設(shè)置空心字顏色時祥诽,必須先將字體高為空心,否則設(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)];
最后是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框架圖
另對于Context的了解可以參考:http://www.padovo.com/blog/2013/01/31/study-coretext/