iOS 使用Iconfont Swift版

关于如何使用Iconfont呢? 首先去Iconfont 选择你需要的图标,下载到本地解压缩,你会看到如下:

iOS 使用Iconfont Swift版_第1张图片

将iconfont.ttf加入到你的工程下面,并且在info.plist中配置你的字体库:

iOS 使用Iconfont Swift版_第2张图片

接下来就是如何使用了,在这里我对此进行了一个简单封装

  • UIFont 扩展
import UIKit
public extension UIFont {
    public class func iconfont(ofSize: CGFloat) -> UIFont? {
        return UIFont(name: "iconfont", size: ofSize)
    }
}
  • UIImage扩展:将Iconfont字体转图片
import Foundation
import UIKit
public extension UIImage {
    public convenience init?(text: Iconfont, fontSize: CGFloat, imageSize: CGSize = CGSize.zero, imageColor: UIColor = UIColor.black) {
        guard let iconfont = UIFont.iconfont(ofSize: fontSize) else {
            self.init()
            return nil
        }
        var imageRect = CGRect(origin: CGPoint.zero, size: imageSize)
        if __CGSizeEqualToSize(imageSize, CGSize.zero) {
            imageRect = CGRect(origin: CGPoint.zero, size: text.rawValue.size(attributes: [NSFontAttributeName: iconfont]))
        }
        UIGraphicsBeginImageContextWithOptions(imageRect.size, false, UIScreen.main.scale)
        defer {
            UIGraphicsEndImageContext()
        }
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = NSTextAlignment.center
        text.rawValue.draw(in: imageRect, withAttributes: [NSFontAttributeName : iconfont, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName: imageColor])
        guard let cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage else {
            self.init()
            return nil
        }
        self.init(cgImage: cgImage)
    }   
}
  • Iconfont 字体库:我们在Swift中需要使用的是Unicode编码所以有了如下:
import Foundation
public enum Iconfont: String {
    case 返回 = "\u{e604}"
    case 速 = "\u{e618}"
    case 设置 = "\u{e665}"
    case 分享 = "\u{e619}"
    case 分享微博 = "\u{e610}"
    case 分享微信好友 = "\u{e611}"
    case 分享微信朋友圈 = "\u{e614}"
    case 分享QQ = "\u{e617}"
    case 跑步608 = "\u{e9cc}"
    case 距离 = "\u{e60c}"
    case kg = "\u{e61b}"
    case 设置_nav = "\u{e632}"
    case 行驶总里程 = "\u{e660}"
    case 统计 = "\u{e679}"
    case 卫星定位 = "\u{e699}"
    case cxm_icon_KM = "\u{e669}"
    case 地图 = "\u{e712}"
    case 返回首页_关闭 = "\u{e789}"
    case 速1 = "\u{e682}"
    case GPS = "\u{e631}"
    case fitness_kg = "\u{e653}"
    case 运动次数 = "\u{e62d}"
    case 里程 = "\u{e6eb}"
    case 运动 = "\u{e600}"
    case gps = "\u{ee6b9}"
    case 时长 = "\u{e648}"
    case 次 = "\u{e629}"
    case 天气图标_晴多云_白天 = "\u{e601}"
    case 天气图标_晴天_白天 = "\u{e602}"
    case 天气图标_晴转多云_昼 = "\u{e603}"
    case 天气图标_晴天_昼 = "\u{e605}"
    case 天气图标_多云 = "\u{e606}"
    case 天气图标_小雨 = "\u{e607}"
    case 天气图标_中雨 = "\u{e608}"
    case 天气图标_大雨 = "\u{e609}"
    case 天气图标_暴雨 = "\u{e60a}"
    case 天气图标_雷阵雨 = "\u{e60b}"
    case 天气图标_小雪 = "\u{e60d}"
    case 天气图标_大雪 = "\u{e60e}"
    case 天气图标_中雪 = "\u{e60f}"
    case 天气图标_暴雪 = "\u{e612}"
    case 天气图标_雨夹雪 = "\u{e613}"
    case 天气图标_冰雹 = "\u{e615}"
    case 天气图标_雾 = "\u{e616}"
    case 天气图标_雾霾 = "\u{e61a}"
    case 天气图标_大风 = "\u{e61c}"
    case 天气图标_扬沙 = "\u{e61d}"
}

当然以上仅仅是部分字体,你也可以添加你工程中需要用到的更多字体库,记得使用Unicode编码即可,在最上面一张图片的demo_unicode.html中可以查看你对应font的Unicode编码

** 使用 **

let label = UILabel()
label.text = Iconfont.qq.rawValue
label.font = UIFont.iconfont(ofSize: 18)

let imageView = UIImageView()
CGSize(width: 30, height: 30)
imageView.image = UIImage(text: Iconfont.qq, fontSize: 30, imageSize: CGSize(width: 30, height: 30), imageColor: UIColor.orange)

你可能感兴趣的:(iOS 使用Iconfont Swift版)