swift - NSMutableAttributedString:如何检索范围内的属性
问题描述
我已经建立了一个简单的游乐场来演示我在检索属性字符串的属性时遇到的问题。也许我不明白如何定义范围:也许我错过了其他东西。
我已经定义了一个 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))
此时字符串看起来很好,可以显示两种颜色的文本。
然后我定义了 3 个范围(尝试不同的方法来定义范围。)
var range1 = NSRange()
var range2 = NSRange(location: 0, length: someText.length)
var range3 = NSRange(location: 40, length: 44)
最后,我尝试检索文本的属性
// 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)
}
我得到以下结果。在所有情况下只显示第一组属性。
attributes1
NSAttributedStringKey(_rawValue: NSColor) UIExtendedSRGBColorSpace 0 0 1 1
NSAttributedStringKey(_rawValue: NSFont) font-family: ".SFUIText-Semibold"; 字体粗细:粗体;字体样式:正常;字体大小:12.00pt
attributes2
NSAttributedStringKey(_rawValue: NSColor) UIExtendedSRGBColorSpace 0 0 1 1 NSAttributedStringKey(_rawValue: NSFont) font-family: ".SFUIText-Semibold"; 字体粗细:粗体;字体样式:正常;字体大小:12.00pt
attributes3
NSAttributedStringKey(_rawValue: NSColor) UIExtendedSRGBColorSpace 0 0 1 1
NSAttributedStringKey(_rawValue: NSFont) font-family: ".SFUIText-Semibold"; 字体粗细:粗体;字体样式:正常;字体大小:12.00pt
我需要做什么才能获取字符串中的所有属性?
解决方案
使用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)")
打印以下结果:italicRange: ["{23, 22}"]
推荐阅读
- android - 如何在 OnSuccessListener 中的 recyclerview 适配器中传递侦听器
- ruby-on-rails - 如何清理此 SQL 查询?
- c++ - 在 std::vector 类中使用迭代器
- zend-framework2 - 如何使用 Zend Framework 2 创建多个 where->like()?
- python - Python GDB 调试 segfault Docker
- amazon-web-services - AWS 简单电子邮件服务未过滤 IP 地址
- ruby-on-rails - RMagick 图像转换
- visual-studio - 在映射为驱动器的 VirtualBox 网络共享上打开问题的解决方案
- node.js - 如何处理导致nodejs崩溃的twilio错误
- tensorflow - 从第一个 epoch 开始,验证准确度和 f1 分数保持不变