本文下方封装了简单的属性字符串方法。封装中只实现了最基础的功能,用来设置一个string中的某些subString特殊显示。调用方法为链式调用。
---> 下面来说说使用NSMutableAttributeString遇到的问题
要特殊显示的subString可以调用
open func addAttributes(_ attrs: [String : Any] = [:], range: NSRange)
来设置,注意这里的range类型是NSRange
,我们在获取这个range的时候想当然会这样做:
//其实这时候获取的range类型是Range
let range = attributeStr.string.range(of: "subString")
Range是无法通过 as? 强制转换到NSRange的。
方式1. 比较简单的处理方式就是
//1.先把String转换成 NSStiring
let nsString = NSString(string: attributeStr.string)
//2.NSString中的range方法获取到的是NSRange类型
let nsRange = nsString.range(of: "subString")
方式2. 另外比较复杂的处理就是把Range转换成NSRange了:
//range转换为NSRange
//扩展的是String类,不可改为NSRange或者Range的扩展,因为samePosition,utf16是String里的
extension String {
func nsRange(from range: Range<String.Index>) -> NSRange {
let from = range.lowerBound.samePosition(in: utf16)
let to = range.upperBound.samePosition(in: utf16)
return NSRange(location: utf16.distance(from: utf16.startIndex, to: from),
length: utf16.distance(from: from, to: to))
}
}
//NSRange转化为range
//扩展的是String类,不可改为NSRange或者Range的扩展,因为utf16是String里的
extension String {
func range(from nsRange: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self)
else { return nil }
return from ..< to
}
}
我的CSDN上同步更新了本文章,点击查看
最终代码:Github代码地址
import Foundation
import UIKit
class TAttributeString {
var attributeStr : NSMutableAttributedString = NSMutableAttributedString()
//设置整体的字符串
func setString(string: String,color: UIColor, fontSize: CGFloat) -> Self {
return setString(string: string, color: color, font: UIFont.systemFont(ofSize: fontSize))
}
func setString(string: String, color: UIColor, font: UIFont) -> Self {
let inceptionAttStr = NSAttributedString(string: string, attributes: [NSFontAttributeName:font,NSForegroundColorAttributeName:color])
attributeStr.setAttributedString(inceptionAttStr)
return self
}
}
//设置属性字符串中的 特殊显示的字符
extension TAttributeString {
func height(string: String, color: UIColor) -> Self {
let nsString = NSString(string: attributeStr.string)
attributeStr.setAttributes([NSForegroundColorAttributeName:color], range: nsString.range(of: string))
return self
}
func height(string: String, color: UIColor, fontSize: CGFloat) -> Self {
return height(string: string, color: color, font: UIFont.systemFont(ofSize: fontSize))
}
func height(string: String, color: UIColor, font: UIFont) -> Self {
let nsString = NSString(string: attributeStr.string)
attributeStr.setAttributes([NSFontAttributeName:font, NSForegroundColorAttributeName:color], range: nsString.range(of: string))
return self
}
}
调用方式
label.attributedText = TAttributeString().setString(string: "swift:这里只是封装了最常用的功能,采用简单的链式调用,核心方法很少,都是一些方便调用的衍生方法", color: UIColor.black, fontSize: 14).height(string: "swift:", color: UIColor.red, fontSize: 20).height (string: "链式调用", color: UIColor.blue, font: UIFont.boldSystemFont(ofSize: 10)).attributeStr
效果: