iOS 使用Iconfont Swift版

關於如何使用Iconfont呢? 首先去Iconfont 選擇你須要的圖標,下載到本地解壓縮,你會看到以下:
html

將iconfont.ttf加入到你的工程下面,而且在info.plist中配置你的字體庫:
swift

接下來就是如何使用了,在這裏我對此進行了一個簡單封裝微信

  • 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)
相關文章
相關標籤/搜索