我有一个带有两个属性的NSAttributedString
从0到5 {0,5} -> attribute1
从6到10 {6,10} -> attribute2
我使用
let attributes = textView.textStorage.attributes(at: 7, longestEffectiveRange: nil, in: NSRange(location: 0, length: textView.textStorage.length))在索引7处获取属性。
在索引7中,可能有完整的属性吗?
在我的例子中,我应该得到6-10
谢谢
发布于 2022-10-14 14:48:26
是的,您可以使用enumerateAttributes提供的NSAttributedString来完成这个任务。意味着获取字符串中每个属性的所有范围,并查找包含索引的范围。
代码会是这样的
var index = 7
attributeString.enumerateAttributes(in: NSRange(location: 0, length: attributeString.length), using: {
_, range, _ in
// if index in range
if range.contains(index) {
print("range: ", range)
// do your code here
}
})https://stackoverflow.com/questions/74070786
复制相似问题