终端字体-彻底指南(填坑)

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

相关文章
相关标签/搜索