首页 > 解决方案 > 如何将自定义字体与映射字形一起使用?

问题描述

我在Illustrator中使用Fontself扩展制作了一些自定义字体。我将字体导出为otf文件,并将字形映射到字母键(a、b、c、d、..)上。我想用它们来显示我的editActionsForRowAt方法的图标而不是字符串。

我按照苹果(和其他指南)提供的步骤将字体包含在我的项目中,所以没有问题。

 override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
        let doc = documentations.value[editActionsForRowAt.row]

        let deleteBtn = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            try? Documentation.db.cascadedDelete(documentationId: doc.id)
            var ary = self.documentations.value
            ary.remove(at: editActionsForRowAt.row)
            self.documentations.accept(ary)
        }
        deleteBtn.backgroundColor = UIColor.init(hexString: "#e53935")

        return [deleteBtn]
    }

我在a上映射了一个Trash -icon并希望显示它而不是“删除”。

如何包含我的Ios-fonts.otf文件以显示图标而不是标题“删除”?

标签: iosswift

解决方案


首先,将字体文件导入您的项目。其次,将字体文件名添加到您的 info.plist 文件(由应用程序提供的字体)中添加字符串值。第三,将它们用于您的代码中,例如:

使用这个静态函数

enum FontStyle: String {
    case regular = "Regular"
    case semiBold = "SemiBold"
    case medium = "Medium"
}

static func applyFonts_SFProText(style: FontStyle, size: CGFloat) -> UIFont {
    return UIFont(name: "SFProText-\(style.rawValue)", size: size)!
}

使用它们的例子:

textLabel.font = applyFonts_SFProText(style: .semiBold, size: 14)

推荐阅读