swift项目中使用阿里iconfont

2016-11-10  本文已影响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.
    }

}
效果截图
上一篇下一篇

猜你喜欢

热点阅读