IconFont
使用 iconfont 通过枚举。目前仅支持 FontAwesome 和自定义 iconfont(您可以从 iconfont.cn、iconmoon 制作自定义 iconfont)
CocoaPods
use_frameworks!
pod 'YFIconFont'
使用
UILabel
label.iconFont(size: 25, icon: FontAwesome.github)
UIButton
button.iconFont(size: 30, icon: FontAwesome.weixin, color: color)
UIImage
我们可以使用fontSize或imageSize来创建UIImage。一种是根据字体大小创建,另一种是根据图像大小创建。
// UIImage size is 39 * 30
imageView.image = UIImage.iconFont(fontSize: 30, icon: FontAwesome.ccvisa, color: color)
// image will scaled to fit with fixed aspect.
// UIImage size is 30 * 30
imageView2.image = UIImage.iconFont(imageSize: CGSize(width: 30, height: 30), icon: FontAwesome.ccvisa, color: color)
UIBarButtonItem
UIBarButtonItem需要指定类型是image或title。
barButtonItem.iconFont(size: 25, icon: FontAwesome.apple, color: color, type: .image)
barButtonItem2.iconFont(size: 25, icon: FontAwesome.apple, color: color, type: .title)
UITabBarItem
leftTabBarItem.image = UIImage.iconFont(fontSize: 25, icon: FontAwesome.chrome)
leftTabBarItem.selectedImage = UIImage.iconFont(fontSize: 25, icon: FontAwesome.chrome, color: color).withRenderingMode(.alwaysOriginal)
leftTabBarItem.title = FontAwesome.chrome.rawValue
rightTabBarItem.image = UIImage.iconFont(fontSize: 25, icon: FontAwesome.firefox)
rightTabBarItem.selectedImage = UIImage.iconFont(fontSize: 25, icon: FontAwesome.firefox, color: color).withRenderingMode(.alwaysOriginal)
rightTabBarItem.title = FontAwesome.firefox.rawValue
自定义图标字体
自定义图标字体应实现IconFontType协议
fontName:字体完全指定的名称。该名称包含字体家族名称和字体特定样式信息。
创建
public enum CustomIconFont: String {
case feedback = "\u{e656}"
case search = "\u{e651}"
case home = "\u{e64f}"
case clock = "\u{e648}"
case like = "\u{e643}"
case shoppingCart = "\u{e63f}"
}
extension CustomIconFont: IconFontType {
public static var fontFilePath: String? = Bundle.main.path(forResource: "iconfont", ofType: "ttf")
public static var fontName: String {
return "iconfont"
}
public var unicode: String {
return self.rawValue
}
}
用法
label1.iconFont(size: 25, icon: CustomIconFont.clock, color:color)
label2.iconFont(size: 30, icon: CustomIconFont.feedback, color: color)
label3.iconFont(size: 35, icon: CustomIconFont.shoppingCart, color: color)