Swift添加字体到项目中

  1. 下载字体,导入到项目文件夹。

  2. 项目Info添加Fonts provided by application的item为字体文件名"xxx.ttf"。

  3. 打印出所有字体名,找到导入的字体文件对应不同weight的字体。如果还是找不到,我也不知道怎么解决。

Swift 复制代码
    for family in UIFont.familyNames.sorted() {
        for fontName in UIFont.fontNames(forFamilyName: family).sorted() {
            print(fontName)
        }
    }
  1. 针对不同weight返回不同字体。
Swift 复制代码
func getEnglishFont(ofSize size: CGFloat, weight: UIFont.Weight) -> UIFont {
        var font: UIFont? = nil
        
        switch weight {
        case .ultraLight:
            font = UIFont(name: "KumbhSans-Regular_ExtraLight", size: size)
        case .thin:
            font = UIFont(name: "KumbhSans-KumbhSans-Regular_Thin", size: size)
        case .light:
            font = UIFont(name: "KumbhSans-Regular_Light", size: size)
        case .regular:
            font = UIFont(name: "KumbhSans-Regular", size: size)
        case .medium:
            font = UIFont(name: "KumbhSans-Regular_Medium", size: size)
        case .semibold:
            font = UIFont(name: "KumbhSans-Regular_SemiBold", size: size)
        case .bold:
            font = UIFont(name: "KumbhSans-Regular_Bold", size: size)
        case .heavy:
            font = UIFont(name: "KumbhSans-Regular_ExtraBold", size: size)
        case .black:
            font = UIFont(name: "KumbhSans-Regular_Black", size: size)
        default:
            print("default font")
            font = UIFont(name: "KumbhSans-Regular", size: size)
        }

        var nonEmptyFont: UIFont = UIFont.systemFont(ofSize: size, weight: weight)
        if let tmpFont = font {
            nonEmptyFont = tmpFont
        } else {
            print("使用默认字体")
        }
        
        return nonEmptyFont
    }
相关推荐
CocoaKier1 天前
苹果谷歌商店:如何监控并维护用户评分评论
ios·google·apple
iOS日常1 天前
iOS设备崩溃日志获取与查看
ios·xcode
wangruofeng2 天前
AI 助力 Flutter 3.27 升级到 3.38 完整指南:两周踩坑与实战复盘
flutter·ios·ai编程
iOS日常2 天前
Xcode 垃圾清理
ios·xcode
开心就好20252 天前
不越狱能抓到 HTTPS 吗?在未越狱 iPhone 上抓取 HTTPS
后端·ios
傅里叶2 天前
iOS相机权限获取
flutter·ios
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言