終端字體-徹底指南(填坑)

iOS

在 iOS 中,若是使用ios

UIFont *font = [[UIFont alloc] init];
複製代碼

建立字體,NSLog 一下會報錯: git

屏幕快照 2019-10-09 上午2.59.43
使用 UIFont 通常不須要 alloc init UIFont 對象,
大部分時候, 咱們使用其類方法來設置對象的屬性。

系統自帶

開發過程當中會頻繁使用:github

UIFont *font1 = [UIFont systemFontOfSize:16];
複製代碼

建立系統字體,
系統字體到底是什麼東西呢?
寫個方法 Log 一下:
bash

logFont

// Font-Playground[705:203000] FamilyName - .SF UI Text
// Font-Playground[705:203000] FullName - System Font Regular
// Font-Playground[705:203000] DisplayName - 系統字體
// Font-Playground[705:203000] PostScriptName - .SFUIText
// Font-Playground[705:203000] ----------------
// Font-Playground[705:203000] <UICTFont: 0x100209350> font-family: ".SFUIText"; font-weight: normal; font-style: normal; font-size: 16.00pt
複製代碼

好吧真叫 System Font,簡稱 .SF。
官網簡介app

後面的 Regular 是字重,
系統字體能夠用自帶的 API 簡便設置字重:
字體

UIFont *font11 = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
UIFont *font12 = [UIFont systemFontOfSize:16 weight:UIFontWeightBlack];
複製代碼

枚舉的 UIFontWeight:網站

const UIFontWeight UIFontWeightUltraLight API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightThin API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightLight API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightRegular API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightMedium API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightSemibold API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightBold API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightHeavy API_AVAILABLE(ios(8.2));
const UIFontWeight UIFontWeightBlack API_AVAILABLE(ios(8.2));
複製代碼

都是 iOS 8.2 以後纔可用的。spa

其餘字體

這裏以 iOS 9 以後自帶的蘋方字體爲例:
(自帶的其餘字體能夠在官網連接中查詢)code

UIFont *font2 = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
複製代碼

廢話不說,打印一下:orm

// Font-Playground[720:206438] FamilyName - PingFang SC
// Font-Playground[720:206438] FullName - PingFang SC Regular
// Font-Playground[720:206438] DisplayName - 蘋方-簡 常規體
// Font-Playground[720:206438] PostScriptName - PingFangSC-Regular
// Font-Playground[720:206438] ----------------
// Font-Playground[720:206438] <UICTFont: 0x10030f160> font-family: "PingFangSC-Regular"; font-weight: normal; font-style: normal; font-size: 16.00pt
複製代碼

能夠看到這裏字重是 Regular,
其餘自帶的字體只能在類方法初始化的時候指定字重:

UIFont *font21 = [UIFont fontWithName:@"PingFangSC-Thin" size:16];
UIFont *font22 = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
複製代碼

這裏安利一個能夠查詢預覽 iOS 中字體的網站:
iOS Fonts Preview
只須要將對應字體名稱輸入,
便可預覽不一樣字重的字體:

iOSfontsShowMax

最後附上 Demo 地址:
Github - UIFont-Playground

相關文章
相關標籤/搜索