mark一个很久以前的问题,当时开发中UI坚持让两端用苹方简体字体Regular格式,那么怎么设置呢?首先苹方字体是iOS系统内置,不用外部导入。其次就是苹方是一个大的字体族,我们要拿到具体有哪些子字体。话不多少,上代码
1.找出所需的具体fontName
//获取所有字体主族name
for fontfamilyname in UIFont.familyNames
{
print("family:",fontfamilyname);
//通过字体主族名称获取所有的子集名称
for fontName in UIFont.fontNames(forFamilyName: fontfamilyname)
{
print("\tfont:",fontName);
}
print("-------------");
}
打印结果,只放出苹方相关的
苹方主族有 PingFang TC,PingFang HK,PingFang SC三种,具体释义就不添加了
...
family: PingFang TC
font: PingFangTC-Regular
font: PingFangTC-Thin
font: PingFangTC-Medium
font: PingFangTC-Semibold
font: PingFangTC-Light
font: PingFangTC-Ultralight
-------------
family: PingFang HK
font: PingFangHK-Medium
font: PingFangHK-Thin
font: PingFangHK-Regular
font: PingFangHK-Ultralight
font: PingFangHK-Semibold
font: PingFangHK-Light
-------------
family: PingFang SC
font: PingFangSC-Medium
font: PingFangSC-Semibold
font: PingFangSC-Light
font: PingFangSC-Ultralight
font: PingFangSC-Regular//UI要求的fontName
font: PingFangSC-Thin
-------------
...
2.设置字体 ps:这里要设置的默认字体根据项目需求是否有需要设置,PingFangSC字体是iOS9.0以后才有的,当时的项目要兼容到iOS7.0
//需要设置的地方直接调用
func setFontWithSize(_ size:CGFloat)->UIFont?{
let font : UIFont? = UIFont(name: "PingFangSC-Regular", size: size) ?? UIFont(name: "HelveticaNeue-Regular", size: size)
return font
}