iOS开发使用iconfont,封装返回UILabel、UIImage、UIImageView

为啥使用iconfont 理由一堆,反正现在就让我用了。 纯色ICon,都用iconfont。
用的多,所以要封装一下。 iconfont可以用于UILabel显示,也可以是UIImage。 我这里封装了一个返回UILabel和UIImageView的,连同带点击事件。
刚开始网上找找有没有封装好的方法,一直都不太好用,干脆自己搞一个吧。
直接上代码
  1. 先获取所有iconfont名称,后面方便直接用
ini 复制代码
public enum IconFontName: String {
    case downLoad = "\u{e60f}"
    case selectRight = "\u{e611}"
    case rightArrow = "\u{e612}"
    case closeIcon = "\u{e614}"
    case listIcon = "\u{e616}" 
    case avater = "\u{e617}"
    case backIcon = "\u{e618}"
    case feedback = "\u{e619}"
    case wrongIcon = "\u{e61b}" 
    case editIcon = "\u{e61c}"
    case warning = "\u{e610}"  
    case score = "\u{e60e}"
    case time = "\u{e60c}"
    case imageIcon = "\u{e60d}"
}

2.返回UILabel,带点击事件

ini 复制代码
class ExIconFont {

    class func iconFontToLabel(frame: CGRect = .zero, fontSize: CGFloat, fontName: String, iconName: String = "", action:(()->Void)?) -> UILabel 
        let lab = UILabel(frame: frame)
        lab.font = R.font.iconfont(size: fontSize)
        lab.text = fontName
        lab.isUserInteractionEnabled = true
        let tap = ClosureTapGestureRecognizer()
        lab.addGestureRecognizer(tap)
        tap.addAction { tap in
            guard let action = action else {
                return
            }
            action()
        }
        return lab
    }

3.返回UILabel有时候不行,那就在加封装返回一个UIImage 或者 UIImageView。 我这里直接返回了UIImageView, 方便使用。

swift 复制代码
class func iconfontToImage(iconText: String, fontSize: CGFloat, fontColor: UIColor, action:(()->Void)?) -> UIImageView {
        // 这里我使用的R.Swift资源管理, 你可以直接用:UIFont(name: "你的iconfont名称", size: fontSize)
        let font = R.font.iconfont(size: fontSize)
        let attributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: fontColor]
        let attributedString = NSAttributedString(string: iconText, attributes: attributes as [NSAttributedString.Key : Any])

        UIGraphicsBeginImageContextWithOptions(CGSize(width: fontSize, height: fontSize), false, 0)
        attributedString.draw(at: CGPoint(x: 0, y: 0))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        // 如果不想用UIIimageView , 这里直接返回image即可。 return image
        
        let imageV = UIImageView(image: image)
        imageV.isUserInteractionEnabled = true
        let tap = ClosureTapGestureRecognizer()
        imageV.addGestureRecognizer(tap)
        tap.addAction { tap in
            guard let action = action else {
                return
            }
            action()
        }
        return imageV
    }

3.自定义一个点击事件封装

swift 复制代码
/// 自定义tapgesture 闭包方式回调
class ClosureTapGestureRecognizer: UITapGestureRecognizer {
    private var action: ((UITapGestureRecognizer) -> Void)?

    func addAction(_ action: @escaping ((UITapGestureRecognizer) -> Void)) {
        self.action = action
    }

    @objc func handleAction(sender: UITapGestureRecognizer) {
        guard let action = action else { return }

        action(sender)
    }

   
    override init(target: Any?, action: Selector?) {
        super.init(target: target, action: action)

        self.addTarget(self, action: #selector(handleAction(sender:)))
    }
}

4.使用方法

php 复制代码
        // 返回label
        let iconLabel = ExIconFont.iconFontLabel(fontSize: 24, fontName: IconFontName.avater.rawValue) {
           self.navigationController?.pushViewController(PersonViewController(), animated: true)
        }
        // 返回UIImageView
        let iconFontImageV = ExIconFont.iconfontToImage(iconText: IconFontName.avater.rawValue, fontSize: 24, fontColor: .black) {
        // 点击事件
           self.navigationController?.pushViewController(PersonViewController(), animated: true)
        }

        // 直接赋值
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: iconFontImageV)

完活~

相关推荐
初级代码游戏12 分钟前
iOS开发 SwiftUI 8:NavigationView 导航
ios·swiftui·swift
美狐美颜SDK开放平台2 小时前
跨平台开发实战:直播美颜sdk动态贴纸在 Android / iOS / HarmonyOS 的落地方案
android·ios·harmonyos·美颜sdk·直播美颜sdk·视频美颜sdk·美颜api
2501_916008892 小时前
在不越狱前提下导出 iOS 应用文件的过程,访问应用沙盒目录,获取真实数据
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915106322 小时前
Android和IOS 移动应用App图标生成与使用 Assets.car生成
android·ios·小程序·https·uni-app·iphone·webview
虹少侠2 小时前
基于 WebKit 构建 macOS 多浮窗视频播放的技术实践(含完整产品落地)
前端·macos·swift·webkit
2501_915918413 小时前
Mac 抓包软件有哪些?Charles、mitmproxy、Wireshark和Sniffmaster哪个更合适
android·ios·小程序·https·uni-app·iphone·webview
2501_915106323 小时前
iOS 抓包绕过 SSL 证书认证, HTTPS 暴力抓包、数据流分析
android·ios·小程序·https·uni-app·iphone·ssl
WeiAreYoung3 小时前
uni-app xcode 制作iOS Notification Service Extension 远程推送图文原生插件
ios·uni-app·xcode
2501_9159214311 小时前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
且去填词18 小时前
Context 详解:如何在微服务链路中传递取消信号与超时控制
ios·iphone