A common IconFont package implemented in Swift to help you use IconFont in your project more conveniently, and integrates a series of free third-party icon libraries.
English Introduction
preview
Example
TestFlight
You can click below TestFlight
button to download the sample program:
Compile the project manually
- use
git clone
command to download this repository; - Use the cd command to switch to the warehouse directory and execute
sh Startup.sh
Order; - then open
Example
under the directoryExample.xcworkspace
Just compile.
or execute the following command:
git clone git@github.com:EFPrefix/EFIconFont.git; cd EFIconFont; sh Startup.sh; open Example/Example.xcworkspace;
need
Version | need |
---|---|
<0.5 | Xcode 10.0+ Swift 4.2+ iOS 8.0+ |
>=0.5 | Xcode 10.2+ Swift 5.0+ iOS 8.0+ |
Install
CocoaPods
EFIconFont can be obtained through CocoaPods.You only need to add the following code to your Podfile to import, the default only contains Core
section, excluding font packs:
The integrated icon library resources of this library can be imported through subspecs. The following examples refer to AntDesign and FontAwesome resources:
pod 'EFIconFont', :subspecs => ['AntDesign', 'FontAwesome']
also via Complete
Introduce all the icon library resources that have been integrated in this library, for example:
pod 'EFIconFont', :subspecs => ['Complete']
Then, execute the following command:
Swift Package Manager
The Swift Package Manager is a tool integrated into the Swift compiler for automating the distribution of Swift code.
If you’ve built your Swift package, it’s easy enough to add EFIconFont as a dependency, just add it to your Package.swift
document dependencies
item can be:
dependencies: [
.package(url: "https://github.com/EFPrefix/EFIconFont.git", .upToNextMinor(from: "1.2.0"))
]
use
1. Core
accomplish EFIconFontProtocol
An object of the protocol, capable of converting itself to NSAttributedString
or UIImage
the content of the agreement is as follows:
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?
}
- name: The font name, which is not necessarily equal to the .ttf file name. You can view its
Name
attribute acquisition; - path: .ttf file path, generally through the form
Bundle.main.path(forResource: name, ofType: "ttf")
(if the file name and name are the same, you do not need to implement this property, just use the default implementation); - attributes: the style of an icon (if there is no special requirement, the default implementation can be used);
- unicode: the unicode of a symbol;
- font: The font size of an icon corresponding to the input fontSize
UIFont
.
Objects that implement this protocol can be converted and output to strings and pictures by calling the following methods, and the foreground color and size can be changed:
// 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. Built-in icon library
This library has integrated AntDesign, FontAwesome and other free icon library resources. Students who need to use can import it. As shown below, you will get an EFIconFontProtocol
Return value of type:
EFIconFontAntDesign.addteam
by following EFIconFontProtocol
Protocol object acquisition NSAttributedString
and UIImage
:
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)
All items of an icon library can be obtained in the following ways, and he will return [String : EFIconFontProtocol]
Dictionary of type:
EFIconFont.antDesign.dictionary
Remarks: Although it is a free icon library, please ensure that your use method follows the usage agreement of the original author of the font library:
3. Custom Icon Library
(1) Import of font files
Convert the icon library we obtained in various ways .ttf
file into the Xcode project and make sure Build Phases
middle Copy Bundle Resources
The font file is included in the list (by default it will be included when dragged into the project).
Also, this file is loaded on demand at runtime and does not need to be added to Info.plist
in the file Fonts provided by application
within the item.
(2) Implementation EFIconFontCaseIterableProtocol
The encapsulation of the icon library can be realized by implementing the EFIconFontCaseIterableProtocol protocol. In this project, Example demonstrates the customization method by taking the Octicons owned by GitHub as an example:
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}"
// ...
}
Some people want to ask how this huge enumeration was typed out by hand?Of course it’s generated by code… first put .svg
Upload pictures to iconfont.cn
then look here.
(3) call
Ditto the use of the built-in icon library.
EFIconFontOcticons.thumbsup
(4) Precautions
The Octicons icon library in the example of this project is owned by GitHub, here is only a demonstration, please do not use it in any occasion that violates the specifications set by its owner:
4. Other
Crawling of some IconFont resource site materials and code generation methods:
author
EyreFree, eyrefree@eyrefree.org
protocol
EFIconFont is distributed and used under the MIT license, see the license file for more information.
#common #IconFont #package #implemented #Swift #IconFont #project #conveniently #integrates #series #free #thirdparty #icon #libraries