版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.03.22 |
前言
iOS圈內(nèi)有幾個人大家基本都知道握玛,比如說王巍寺惫、唐巧菇晃,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等祈远。這里有一篇唐巧對他的專訪呆万,還有他的 GitHub - Yaoyuan 和 博客,這里貼出來框架YYKit 框架车份。接下來幾篇我們就一起來看一下這個框架谋减。感興趣的可以看上面寫的幾篇。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類之Hash(一)
3. YYKit源碼探究(三) —— NSString分類之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(一)
9. YYKit源碼探究(九) —— UIFont分類之架構(gòu)分析和Font Traits(一)
回顧
上一篇主要介紹了UIFont分類的架構(gòu)以及第一部分Font Traits
的說明扫沼,這一篇我們就看一下Create font
部分出爹。
API接口
下面我們看一下API接口庄吼。
/**
Creates and returns a font object for the specified CTFontRef.
@param CTFont CoreText font.
*/
+ (nullable UIFont *)fontWithCTFont:(CTFontRef)CTFont;
/**
Creates and returns a font object for the specified CGFontRef and size.
@param CGFont CoreGraphic font.
@param size Font size.
*/
+ (nullable UIFont *)fontWithCGFont:(CGFontRef)CGFont size:(CGFloat)size;
/**
Creates and returns the CTFontRef object. (need call CFRelease() after used)
*/
- (nullable CTFontRef)CTFontRef CF_RETURNS_RETAINED;
/**
Creates and returns the CGFontRef object. (need call CFRelease() after used)
*/
- (nullable CGFontRef)CGFontRef CF_RETURNS_RETAINED;
1. + (nullable UIFont *)fontWithCTFont:(CTFontRef)CTFont;
該方法的作用就是根據(jù)指定的CTFontRef
類型字體返回一個UIFont
字體。
下面不給出方法的舉例严就,直接看一下方法的實現(xiàn)总寻。
+ (UIFont *)fontWithCTFont:(CTFontRef)CTFont {
if (!CTFont) return nil;
CFStringRef name = CTFontCopyPostScriptName(CTFont);
if (!name) return nil;
CGFloat size = CTFontGetSize(CTFont);
UIFont *font = [self fontWithName:(__bridge NSString *)(name) size:size];
CFRelease(name);
return font;
}
2. + (nullable UIFont *)fontWithCGFont:(CGFontRef)CGFont size:(CGFloat)size;
該方法的作用就是根據(jù)指定的CGFontRef
對象以及對應(yīng)的尺寸,獲得UIFont對象梢为。
+ (UIFont *)fontWithCGFont:(CGFontRef)CGFont size:(CGFloat)size {
if (!CGFont) return nil;
CFStringRef name = CGFontCopyPostScriptName(CGFont);
if (!name) return nil;
UIFont *font = [self fontWithName:(__bridge NSString *)(name) size:size];
CFRelease(name);
return font;
}
3. - (nullable CTFontRef)CTFontRef
該方法的作用就是根據(jù)UIFont對象獲取對應(yīng)的CTFontRef對象渐行。但是看一下說明。
Creates and returns the CTFontRef object. (need call CFRelease() after used)铸董。也就是說需要調(diào)用CFRelease() 進行釋放祟印。
下面看一下該方法的實現(xiàn)。
- (CTFontRef)CTFontRef CF_RETURNS_RETAINED {
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.fontName, self.pointSize, NULL);
return font;
}
4. - (nullable CGFontRef)CGFontRef
該方法的作用也就是根據(jù)UIFont對象獲取對應(yīng)的CGFontRef
對象粟害。但是還是需要注意下面這句話蕴忆。
Creates and returns the CGFontRef object. (need call CFRelease() after used)。也就是說需要調(diào)用CFRelease()進行釋放悲幅。
下面看一下方法的實現(xiàn)套鹅。
- (CGFontRef)CGFontRef CF_RETURNS_RETAINED {
CGFontRef font = CGFontCreateWithFontName((__bridge CFStringRef)self.fontName);
return font;
}
后記
本篇主要介紹了UIFont的幾種實例化方法,包括通過CTFontRef和CGFontRef創(chuàng)建實例的幾個方法汰具。