[iOS 开源库] EFIconFont - 一个 Swift 实现的 IconFont 封装,帮助你便捷地在你的工程中使用 IconFont

2019-03-25 11:31:51 +08:00
 EyreFree

一个用 Swift 实现的普通的 IconFont 封装,帮助你更便捷地在你的工程中使用 IconFont,同时集成了一系列可免费使用的第三方图标库。

English Introduction

示例

  1. 利用 git clone 命令下载本仓库;
  2. 利用 cd 命令切换到 Example 目录下,执行 pod install 命令;
  3. 随后打开 EFIconFont.xcworkspace 编译即可。

或执行以下命令:

git clone git@github.com:EFPrefix/EFIconFont.git; cd EFIconFont/Example; pod install; open EFIconFont.xcworkspace

需求

安装

EFIconFont 可以通过 CocoaPods 进行获取。只需要在你的 Podfile 中添加如下代码就能实现引入,默认只包含 Core 部分,不含字体包:

pod 'EFIconFont'

可以通过 subspecs 方式引入本库已集成的图标库资源,如下示例引用了 AntDesign 和 FontAwesome 资源:

pod 'EFIconFont', :subspecs => ['AntDesign', 'FontAwesome']

也可以通过 Complete 引入本库已集成的所有图标库资源,示例:

pod 'EFIconFont', :subspecs => ['Complete']

然后,执行如下命令即可:

pod install

使用

1. 核心

实现 EFIconFontProtocol 协议的对象,能够将自身转换为 NSAttributedStringUIImage,该协议内容如下:

public protocol EFIconFontProtocol {

    // `name` is not necessarily equal to .ttf file name
    var name: String { get }

    // `path` is path of .ttf file
    var path: String { get }

    // `attributes` is style of icon
    var attributes: [NSAttributedString.Key : Any] { set get }

    // `unicode` is unique identifier of particular icon
    var unicode: String { get }
}

实现该协议的对象,可通过调用下列方法进行转换输出为字符串和图片,可改变前景色和大小:

// MARK:- String
func attributedString(size fontSize: CGFloat, attributes: [NSAttributedString.Key : Any]?) -> NSAttributedString?
func attributedString(size fontSize: CGFloat, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> NSAttributedString?

// MARK:- Image
func image(size fontSize: CGFloat, attributes: [NSAttributedString.Key : Any]?) -> UIImage?
func image(size fontSize: CGFloat, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> UIImage?
func image(size imageSize: CGSize, attributes: [NSAttributedString.Key : Any]?) -> UIImage?
func image(size imageSize: CGSize, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> UIImage?

2. 自带图标库

本库已集成了 AntDesign、FontAwesome 等免费图标库资源,需要使用的同学引入即可,如下所示,会得到一个 EFIconFontProtocol 类型的返回值:

EFIconFontAntDesign.addteam

可通过遵循 EFIconFontProtocol 协议的对象获取 NSAttributedStringUIImage

EFIconFontAntDesign.addteam.attributedString(size: 24)
EFIconFontFontAwesomeBrands.adobe.attributedString(size: 32, foregroundColor: UIColor.white, backgroundColor: UIColor.green)
EFIconFontFontAwesomeRegular.addressBook.image(size: 24, foregroundColor: UIColor.red)
EFIconFontFontAwesomeSolid.alignLeft.image(size: CGSize(width: 36, height: 48), foregroundColor: UIColor.white)

可通过如下方式获取某个图标库的全部项目,他会返回 [String : EFIconFontProtocol] 类型的 Dictionary:

EFIconFont.antDesign.dictionary

备注:虽为免费图标库,但还请自行确保您的使用方式遵循字库原始作者的使用协议规范。

3. 自定义图标库

(1) 字体文件引入

将我们通过各种方式获取的图标库的 .ttf 文件拖入 Xcode 工程中,并确保 Build Phases 中的 Copy Bundle Resources 列表中包含这个字体文件(默认拖入工程就会被包含在内)。

另外,此文件会在运行时按需加载,无需添加到 Info.plist 文件中的 Fonts provided by application 项内。

(2) 实现 EFIconFontCaseIterableProtocol

可通过实现 EFIconFontCaseIterableProtocol 协议实现图标库的封装,本项目中 Example 以 GitHub 所有的 Octicons 为例 演示 了自定义方式:

import EFIconFont

public extension EFIconFont {
    public static let octicons = EFIconFontOcticons.self
}

extension EFIconFontOcticons: EFIconFontCaseIterableProtocol {
    public static var name: String {
        return "octicons"
    }
    public var unicode: String {
        return self.rawValue
    }
}

public enum EFIconFontOcticons: String {
    case thumbsup = "\u{e6d7}"
    case unverified = "\u{e6d6}"
    case unfold = "\u{e6d5}"
    case verified = "\u{e6d4}"
    // ...
}

有人要问这个巨长无比的枚举是怎么手打出来的?当然是代码生成的了...先把 .svg 图片上传到 iconfont.cn,然后看 这里

(3) 调用

同上自带图标库的使用。

EFIconFontOcticons.thumbsup

(4) 注意事项

本项目 Example 中的 Octicons 图标库为 GitHub 所有,此处仅为演示,请勿用于任何违反其所有者所定规范的场合:GitHub Logos and Usage

4. 其它

一些 IconFont 资源站点素材的爬取以及代码生成方式:

作者

EyreFree, eyrefree@eyrefree.org

协议

EFIconFont 基于 MIT 协议进行分发和使用,更多信息参见 协议文件


#iOS 开源库# EFIconFont 0.4.1 发布啦,🎉

EFIconFont 是一个用 Swift 实现的普通的 IconFont 封装,帮助你更便捷地在你的工程中使用 IconFont,同时集成了一系列可免费使用的第三方图标库,如 ElusiveIcons、FontAwesome、以及 Google 家的 MaterialIcons 等,详情可参考 Demo 工程及文档。

欢迎大佬们使用、反馈,好用的话别忘了点个小星星,💥...仓库地址:https://github.com/EFPrefix/EFIconFont

4339 次点击
所在节点    iDev
4 条回复
designer
2019-03-25 11:35:00 +08:00
这个项目不错哦,支持下。
顺便广告下自己画的一套矢量像素图标: http://chuangzaoshi.com/icon/
DamonYu
2019-04-02 18:22:37 +08:00
DA 佬你来啦
EyreFree
2019-04-02 18:26:05 +08:00
@DamonYu 大佬好,[扑通]
EyreFree
2021-06-28 15:46:25 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/548223

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX