前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS使用IconFont

iOS使用IconFont

作者头像
莫空9081
修改2023-09-24 19:25:38
1.6K0
修改2023-09-24 19:25:38
举报
文章被收录于专栏:iOS 备忘录

IconFont的使用

github链接

背景: 最近设计提了要求,切图用IconFont

一开始,在搜了IconFont的使用后,选中了TBCityIconFont,使用了之后,发现对于图片的支持不太好,就是长宽不等的图片,显示的时候会被截断,在具体实现里也没发现修改的地方。

然后就pass了,换了另一个Swift的Iconic的第三方库,这个库有个好处,是可以自动把.ttf文件里的Icon生成一个枚举使用的时候特别方便。然而缺点是安装的时候麻烦,之前只是稍微麻烦,最近变得特别麻烦,需要设置FONT_PATH。而且不支持最新版本的Swift,每次更新.ttf字体文件,都要改一次。

实在是受不了了,今天就抽空自己写(抄)了一个。?有兴趣的可以自己看参考链接,其实就是参考链接里内容的整合。

实现

首先,我想要的是,传入一个Int类型(eg: 0xe654)或者字符串类型(eg :\u{E61A})都支持。 然后如果是本地的文件,我希望可以不用手动收入unicode码。最后就是图片支持宽高不等的显示。

整体的原理是Working with icon fonts in iOS. Code example in Swift 3.和github Iconic的结合。

首先是传入类型的支持,定义两个类方法,一个传入Int,一个传入String,然后再实现一个Int->String的方法,然后根据String实现具体内容。

本地文件每次收入unicode吗不方便,定义两个枚举类型,一个是enum string,一个是enum UInt32,定义UInt32是为了可以兼容OC。如果纯Swift只需要使用enum String即可。

图片宽高不等的显示:关键代码如下

代码语言:javascript
复制

...xxx
        var rect = CGRect(origin: CGPoint.zero, size: size)
        rect.origin.y -= edgeInsets.top
        rect.size.width -= edgeInsets.left + edgeInsets.right // 运算符优先级注意
        rect.size.height -= edgeInsets.top + edgeInsets.bottom

...xxx

使用

  1. 来自iconfont怎么在iOS中使用了
  1. 将MWIconFont.Swift添加到项目中
  2. 修改文件中两个地方,
  1. 使用

Swift label使用,生成attributeString

代码语言:javascript
复制
// 使用枚举Str
let attributeStr = MWIconFont.attributedString(fromIconStr: MWFontIcon.yuedufuHuodeIcon.rawValue, size: 50.0, color: UIColor.red)
displayLabel.attributedText = attributeStr

// 使用枚举Int
let attributeStr1 = MWIconFont.attributedString(fromIconInt: MWFontIconInt.yuedufuHuodeIcon.rawValue, size: 50.0, color: UIColor.blue)
displayLabel.attributedText = attributeStr1

OC label使用,生成attributeString

代码语言:javascript
复制
NSMutableString *jiantouStr = [MWIconFont attributedStringFromIconInt:MWFontIconIntZuojiantouIcon size:CGSizeMake(19.0, 19.0) color:[UIColor redColor]];

Swift 生成image

代码语言:javascript
复制
// 使用枚举Str
let image =  MWIconFont.image(fromIconStr: MWFontIcon.yuedufuHuodeIcon.rawValue, size: CGSize(width: 30.0, height: 50.0), color: UIColor.cyan, edgeInsets: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: -10.0))
displayImageView.image = image

// 使用枚举Int
let image1 = MWIconFont.image(fromIconInt: MWFontIconInt.yuedufuHuodeIcon.rawValue, size: CGSize(width: 30.0, height: 50.0), color: UIColor.blue, edgeInsets:  UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: -10.0))
displayImageView.image = image1

OC 生成image

代码语言:javascript
复制
UIImage *backIcon = [MWIconFont imageFromIconInt:MWFontIconIntZuojiantouIcon size:CGSizeMake(19.0, 19.0) color:[UIColor redColor]];

参考链接

Working with icon fonts in iOS. Code example in Swift 3.

iconfont怎么在iOS中使用了

Iconfont-阿里巴巴矢量图标库

github Iconic

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-022,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • IconFont的使用
    • github链接
      • 背景: 最近设计提了要求,切图用IconFont
        • 实现
          • 使用
            • 参考链接
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档