NSMutableAttributedString:如何检索一定范围内的属性

jz_*_*jz_ 4 nsattributedstring swift

我建立了一个简单的游乐场来演示我在检索属性字符串的属性时遇到的问题。也许我不明白如何定义范围:也许我错过了其他东西。

我定义了一个 NSMutableAttributedString,它有两种颜色,并将字体更改为粗体(蓝色)和斜体(红色):

var someText =  NSMutableAttributedString()

let blueAttri : [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12), NSAttributedString.Key.foregroundColor : UIColor.blue]
let redAttri : [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 12), NSAttributedString.Key.foregroundColor : UIColor.red]

someText = NSMutableAttributedString(string: "Some blue string here; ", attributes: blueAttri)
someText.append(NSMutableAttributedString(string: "Some red string here; ", attributes: redAttri))
Run Code Online (Sandbox Code Playgroud)

此时,字符串看起来很好,显示两种颜色的文本。

然后我定义了 3 个范围(尝试不同的方法来定义范围。)

var range1 = NSRange()
var range2 = NSRange(location: 0, length: someText.length)
var range3 = NSRange(location: 40, length: 44)
Run Code Online (Sandbox Code Playgroud)

最后,我尝试检索文本的属性

// retrieve attributes
let attributes1 = someText.attributes(at: 0, effectiveRange: &range1)

// iterate each attribute
print("attributes1")
for attr in attributes1 {
    print(attr.key, attr.value)
}

let attributes2 = someText.attributes(at: 0, effectiveRange: &range2)

// iterate each attribute
print("attributes2")
for attr in attributes2 {
    print(attr.key, attr.value)
}


let attributes3 = someText.attributes(at: 0, effectiveRange: &range3)

// iterate each attribute
print("attributes3")
for attr in attributes3 {
    print(attr.key, attr.value)
}
Run Code Online (Sandbox Code Playgroud)

我得到以下结果。在所有情况下仅显示第一组属性。

属性1
NSAttributedStringKey(_rawValue: NSColor) UIExtendedSRGBColorSpace 0 0 1 1
NSAttributedStringKey(_rawValue: NSFont) 字体系列:“.SFUIText-Semibold”;字体粗细:粗体;字体样式:正常;字体大小:12.00pt

attribute2
NSAttributedStringKey(_rawValue: NSColor) UIExtendedSRGBColorSpace 0 0 1 1 NSAttributedStringKey(_rawValue: NSFont) 字体系列:“.SFUIText-Semibold”;字体粗细:粗体;字体样式:正常;字体大小:12.00pt

attribute3
NSAttributedStringKey(_rawValue: NSColor) UIExtendedSRGBColorSpace 0 0 1 1
NSAttributedStringKey(_rawValue: NSFont) 字体系列:“.SFUIText-Semibold”;字体粗细:粗体;字体样式:正常;字体大小:12.00pt

我需要做什么才能获取字符串中的所有属性?

有人建议我使用枚举属性。这似乎不合法。见下文: 在此输入图像描述

jz_*_*jz_ 7

使用enumerateAttributes我能够捕获斜体范围(仅举一个例子)。以下是完整代码:

//Create Empty Dictionaries for storing results
var attributedFontRanges = [String: UIFont]()
var attributedColorRanges = [String: UIColor]()

//Find all attributes in the text.
someText.enumerateAttributes(in: NSRange(location: 0, length: someText.length)) { (attributes, range, stop) in

    attributes.forEach { (key, value) in
        switch key {

        case NSAttributedString.Key.font:
            attributedFontRanges[NSStringFromRange(range)] = value as? UIFont

        case NSAttributedString.Key.foregroundColor:
            attributedColorRanges[NSStringFromRange(range)] = value as? UIColor

        default:

            assert(key == NSAttributedString.Key.paragraphStyle, "Unknown attribute found in the attributed string")
        }
}
}

//Determine key(range) of Italic font
var italicRange = attributedFontRanges.filter { $0.value.fontName == ".SFUIText-Italic" }.keys

print("italicRange: \(italicRange)")
Run Code Online (Sandbox Code Playgroud)

打印以下结果: italicRange: ["{23, 22}"]