美文网首页
swift项目中使用阿里iconfont

swift项目中使用阿里iconfont

作者: 墨狂之逸才 | 来源:发表于2016-11-10 07:35 被阅读58次

    另外关于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.
        }
    
    }
    
    效果截图

    相关文章

      网友评论

          本文标题:swift项目中使用阿里iconfont

          本文链接:https://www.haomeiwen.com/subject/yigtpttx.html