一个普通的 iconfont cocoapods 包,可以帮助你在 Swift 项目中更轻松地使用 iconfont。

中文介绍

预览

1 2 3 4

示例

要手动运行示例项目,请克隆 repo,然后在项目文件夹中运行 sh Startup.sh。 演示位于 Example 文件夹中,使用 Xcode 打开 Example.xcworkspace 并选择您想要的目标,然后运行。

或者,您可以在终端中运行以下命令

git clone git@github.com:EFPrefix/EFIconFont.git; cd EFIconFont; sh Startup.sh; open Example/Example.xcworkspace;

要求

安装

CocoaPods

EFIconFont 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中

pod 'EFIconFont'

您可以使用 subspecs 获取内置的 iconfont,例如,您可以通过以下方式获取 AntDesignFontAwesome 的图标

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

您也可以选择使用 Complete subspec 获取所有内置包

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

然后,运行以下命令

pod install

Swift 包管理器

Swift 包管理器是一种用于自动分发 Swift 代码的工具,并已集成到 Swift 编译器中。

设置好 Swift 包后,将 EFIconFont 添加为依赖项就像将其添加到 Package.swiftdependencies 值中一样容易。

dependencies: [
    .package(url: "https://github.com/EFPrefix/EFIconFont.git", .upToNextMinor(from: "6.0.0.0"))
]

使用

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 }
    
    // `font` is UIFont of icon with input font size
    func font(size fontSize: CGFloat) -> UIFont?
}

实现该协议的对象可以通过调用以下方法转换为字符串和图像,您还可以更改前景色和大小

// 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. 内置 iconfont 包

此 pod 在 subspecs 中集成了一些免费资源,如 AntDesignFontAwesome。 它可以被需要使用它的人导入。 使用方法如下,您可以获得 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]EFIconFontCaseIterableProtocol 对象的所有图标

EFIconFont.antDesign.dictionary

PS: 尽管以下库都是免费的,请确保您使用图标的方式符合原始作者的协议规范

名称 版本 数量 文件大小 描述 许可证 预览
AliCloudConsole 1.0 266 42KB AliCloudConsole 未知 iconfont.cn
AntChain 1.0 77 17KB AntChain 未知 iconfont.cn
AntDesign 1.0 557 127KB Ant Design MIT iconfont.cn
Dashicons 0.9.0 203 51 KB WordPress 管理的官方图标字体 GPLv2 wordpress.org
Devicons 1.8.0 192 92KB 开发人员的标志性字体 MIT vorillaz.github.io/devicons
ElusiveIcons 2.0.0 304 53KB Elusive Icons OFL elusiveicons.com
EVAIcon 21 45 KB EVA OFL EVA-icon
EvilIcons 1.10.1 70 16 KB 简单干净的 SVG 图标包 MIT evil-icons.io
FontAwesom(Regular / Brands / Solid) 5.8.1 1516 356KB Font Awesome Font Awesome Free License fontawesome.com
FoundationIcons 3.0 283 68KB Foundation Icon Fonts 3 - zurb.com/playground/foundation-icon-fonts-3
Genericons 4.0.4 103 16KB Genericons Neue 是通用外观的图标,适用于博客或简单的网站 GPLv2 genericons.com
Hawcons 1.0 1035 685KB Hawcons,包括文档,运动,天气,表情符号,手势,文件类型等的图标。 免费 hawcons.com
IcoMoon 1.0 490 94KB IcoMoon 免费图标 CC BY 4.0 / GPL ionicons.com
Ionicons 4.5.5 696 143KB Ionicons MIT ionicons.com
LigatureSymbols 2.11 239 84KB Ligature Symbols OFL kudakurage.com/ligature_symbols
MapIcons 3.0.0 192 48 KB 一组用于地图的图标 OFL map-icons.com
MaterialIcons 3.0.1 1057 695KB Google 的 material design 图标,四种样式 Apache-2.0 material.io
Meteocons 47 15KB 一组天气图标,包含 40 多个图标 免费 alessioatzeni.com/meteocons
MetrizeIcons 1.0 300 74KB 设计师和开发人员的 300 个 Metro 风格图标的免费集合 免费 alessioatzeni.com/metrize-icons
OpenIconic 1.1.1 223 33KB 一个开源图标集,在 SVG 中有 223 个标记 OFL useiconic.com/open
StrawberryIcon 2.0.0 382 69KB 一个为开发者和创造者提供的免费且开源的图标字体库 OFL chuangzaoshi.com/icon
Stroke7 1.2.0 202 44KB 一系列受 iOS 7 启发的细笔画图标 免费 themes-pixeden.com/font-demos
淘宝 1.0 434 126KB 淘宝 未知 iconfont.cn
ThemifyIcons 1.0 352 68KB 一套完整的图标,可在网页设计和应用程序中使用 免费 themify.me
天猫 1.0 564 190KB 天猫 未知 iconfont.cn
Typicons 2.0.9 336 82KB 套件中有 336 个像素完美的通用矢量图标 OFL s-ings.com/typicons
VSCodeIcons 1.0 133 24KB Visual Studio Code 的图标 MIT github.com/microsoft/vscode-icons
WeatherIcons 2.0.10 219 64KB 基于天气、海事和气象的图标 OFL erikflowers.github.io/weather-icons
Zocial 1.3.0 112 31KB 流行的社交图标 MIT smcllns.github.io/css-social-buttons/

3. 扩展自定义包

(1) 导入字体文件

将图标库的 .ttf 文件拖到 Xcode 项目中,并确保 Build Phases 中的 Copy Bundle Resources 列表包含此字体文件(默认情况下会包含,只需检查即可)。

此外,该文件将在运行时加载,无需将其添加到 Info.plist 文件中的 Fonts provided by application 项目中。

(2) 实现 EFIconFontCaseIterableProtocol

通过实现 EFIconFontCaseIterableProtocol,您可以获得您的自定义 iconfont 包对象,该项目中的演示展示了使用 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}"
    // ...
}

(3) 调用

与上面的 Built-in iconfont packs 相同

EFIconFontOcticons.thumbsup

(4) 注意

本项目中的 Octicons 图标库归 GitHub 所有。这只是一个演示,请勿在任何违反其所有者设定的规范的情况下使用它

名称 版本 数量 文件大小 描述 许可证 预览
Octicons 8.4.2 184 34KB GitHub 的图标 GitHub Logos and Usage octicons.github.com

4. 其他

一些 iconfont 资源站点的使用

作者

EyreFree, eyrefree@eyrefree.org

许可证

EFIconFont 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。