美文网首页
Swift富文本

Swift富文本

作者: 小笨憨 | 来源:发表于2017-04-05 17:15 被阅读0次

字符串中单个变色

extension String {
 public func getAttributedString(sender:String) -> NSMutableAttributedString {
        
        let attributedString = NSMutableAttributedString.init()
        
        let jndex   = self.range(of: sender)
        let before = self.substring(to: (jndex?.lowerBound)!)
        
        let beforeAttributedString = NSAttributedString.init(string: before, attributes: [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 16),NSForegroundColorAttributeName:UIColor.black])
        attributedString.append(beforeAttributedString)
        
        let titleAttributedString = NSAttributedString.init(string: sender, attributes: [NSForegroundColorAttributeName:UIColor.cyan,NSFontAttributeName:UIFont.boldSystemFont(ofSize: 16)])
        attributedString.append(titleAttributedString)
        
        let upper = self.substring(from: (jndex?.upperBound)!)
        
        let contentAttributedString = NSAttributedString.init(string: upper, attributes: [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 16),NSForegroundColorAttributeName:UIColor.black])
        
        attributedString.append(contentAttributedString)
        return attributedString
    }
}

相关文章

网友评论

      本文标题:Swift富文本

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