swift项目中使用阿里iconfont

另外关于iconfont的设置阿里iconfont官网帮助文档有说明,具体参考
IOS使用iconfont
还有使用iconfont的好处这里也不多做介绍。
直接上代码:

class ViewController: UIViewController {
    var iconfontText: String = "\u{e620}"
    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        let content = "   墨狂之逸才"
        let textStr = iconfontText + content
        let contentLength = content.characters.count
        let stringLength = textStr.characters.count
        let iconfontLength = iconfontText.characters.count
        print("stringLength:\(stringLength)---iconfontLength:\(iconfontLength)---contentLength:\(contentLength)")
        //设置富文本
        let attributeStr:NSMutableAttributedString=NSMutableAttributedString(string: textStr)
        attributeStr.addAttribute(NSFontAttributeName, value: UIFont(name: "iconfont", size: 16)!, range: NSMakeRange(0, iconfontLength))
        attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, iconfontLength))
        
        attributeStr.addAttribute(NSBackgroundColorAttributeName, value: UIColor.blue, range: NSRange(
            location:0,
            length:iconfontLength))
        
        //文本0开始5个字符字体HelveticaNeue-Bold,16号
        attributeStr.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(iconfontLength, stringLength - iconfontLength))
        //设置字体颜色
        attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(iconfontLength, stringLength - iconfontLength))
        
        //设置文字背景颜色
//        attributeStr.addAttribute(NSBackgroundColorAttributeName, value: UIColor.green, range: NSMakeRange(0, 4))
        label.attributedText = attributeStr
        
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
效果截图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Iconfont 是阿里妈妈MUX倾力打造的矢量图标管理、交流平台, 设计师可以将图标上传到Iconfont平台,...
    望星star阅读 10,233评论 0 0
  • 今天是我坚持做头脑的主人,重塑内在力量的第2天,今天我要练习的主题是感恩,我相信我坚持的每一步,都会让我更爱自己,...
    内外合一阅读 903评论 0 1
  • 冠捷大厦社区 虹桥冠捷空间位于大虹桥核心商务区,500强企业云集,交通便利轨交2.10.17号线直达上海市区,高铁...
    3人办公阅读 2,784评论 0 0
  • 在我们的工作中,升职快的总是那些情商高的人;在生活中,受到大家欢迎的也总是那些情商高的人。拥有了高情商的人,人生好...
    97的方菇凉阅读 2,939评论 0 1
  • 我所说的“安慰”并不是朋友亲人面对面给你说的一些开导的话,而是通过看到的感受到的东西形成的一种心理上的释怀。 这种...
    unmi阅读 2,994评论 0 0