博客> 关于用Swift 4.0设置Label多彩文字AttributedText问题
关于用Swift 4.0设置Label多彩文字AttributedText问题
2019-11-14 03:14 评论:0 阅读:510 滑板小王子
swift 富文本 AttributedText

最近由于开发需要,需要将Label的文字设置才如图样式

 1524101713535.jpg

由于使用的swift开发,所以从网上搜索,大部分答案是这样的

        let minuteString = String(15)
        let secondString = String(54)
        let hintString = NSMutableAttributedString(string: minuteString + "分" + secondString + "秒")
        //获取要调整颜色的文字位置,调整颜色
        let range1: NSRange = (hintString.string as NSString).range(of:minuteString)
        hintString.addAttribute(kCTForegroundColorAttributeName as NSAttributedStringKey, value: UIColorFromRGB(rgbValue: 0xFF8617), range: range1)
        let range2: NSRange = (hintString.string as NSString).range(of:secondString)
        hintString.addAttribute(kCTForegroundColorAttributeName as NSAttributedStringKey, value: UIColorFromRGB(rgbValue: 0xFF8617), range: range2)
       myLabel.attributedText = hintString

然而没什么效果,很无奈是不是,最后修改如下,重点: 将kCTForegroundColorAttributeName as NSAttributedStringKey 改为 NSAttributedStringKey.foregroundColor

改完的整段代码是:

        let minuteString = String(15)
        let secondString = String(54)
        let hintString = NSMutableAttributedString(string: minuteString + "分" + secondString + "秒")
        //获取要调整颜色的文字位置,调整颜色
        let range1: NSRange = (hintString.string as NSString).range(of:minuteString)
        hintString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0xFF8617), range: range1)
        let range2: NSRange = (hintString.string as NSString).range(of:secondString)
        hintString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0xFF8617), range: range2)
        myLabel.attributedText = hintString

出效果了,走你

收藏
1
sina weixin mail 回到顶部