从 NSAttributedString 获取文本块
Get blocks of text from NSAttributedString
如果我使用
从文本字段中获取属性字符串
let text = input.attributedText!
print(text)
in Swift,则输出如下(当输入包含正则"hello"则粗体显示"world")
hello {
NSColor = "UIDeviceWhiteColorSpace 0 1";
NSFont = "<UICTFont: 0x134544930> font-family: \".SFUIText-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 2, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSShadow = "NSShadow {0, -1} color = {(null)}";
}
world{
NSColor = "UIDeviceWhiteColorSpace 0 1";
NSFont = "<UICTFont: 0x1345b12a0> font-family: \".SFUIText-Bold\"; font-weight: bold; font-style: normal; font-size: 17.00pt";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 2, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSShadow = "NSShadow {0, -1} color = {(null)}";
}
我可以看到当打印到控制台时,这两个不同格式的书写块以两个块表示。现在我想做的是遍历所有块,并为每个块获取文本和字体。所以在这种情况下,第一次循环时,它会找到 "Hello" 和 " font-family: \".SFUIText-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt",第二次它会找到 "world" 并且它是 font
我可以使用代码循环浏览字体
text.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, text.length), options: NSAttributedStringEnumerationOptions()) { (font: AnyObject?, range: NSRange, usmp: UnsafeMutablePointer<ObjCBool>) -> Void in
print(font)
}
有没有办法对实际文本做同样的事情?
是 - 只需枚举所有属性而不是一个。
假设您有一个像这样的属性字符串:
// Create the string
let text = NSMutableAttributedString(string: "hello world")
let font = UIFont(name: ".SFUIText-Regular", size: 17)!
let boldFont = UIFont(name: ".SFUIText-Bold", size: 17)!
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, text.string.characters.count))
text.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, 6))
text.addAttribute(NSFontAttributeName, value: boldFont, range: NSMakeRange(6, 5))
您可以像这样创建类似于示例中的输出:
// Enumerate the attributes
text.enumerateAttributesInRange(NSMakeRange(0, text.string.characters.count), options: []) { (attribute, range, stop) -> Void in
let substring = (text.string as NSString).substringWithRange(range)
debugPrint(substring, attribute)
}
输出如下所示:
"hello " ["NSFont": <UICTFont: 0x7fba19724be0> font-family: ".SFUIText-Regular"; font-weight: normal; font-style: normal; font-size: 17.00pt, "NSColor": UIDeviceWhiteColorSpace 0 1]
"world" ["NSFont": <UICTFont: 0x7fba1960d8d0> font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 17.00pt, "NSColor": UIDeviceWhiteColorSpace 0 1]
如果我使用
从文本字段中获取属性字符串let text = input.attributedText!
print(text)
in Swift,则输出如下(当输入包含正则"hello"则粗体显示"world")
hello {
NSColor = "UIDeviceWhiteColorSpace 0 1";
NSFont = "<UICTFont: 0x134544930> font-family: \".SFUIText-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 2, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSShadow = "NSShadow {0, -1} color = {(null)}";
}
world{
NSColor = "UIDeviceWhiteColorSpace 0 1";
NSFont = "<UICTFont: 0x1345b12a0> font-family: \".SFUIText-Bold\"; font-weight: bold; font-style: normal; font-size: 17.00pt";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 2, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSShadow = "NSShadow {0, -1} color = {(null)}";
}
我可以看到当打印到控制台时,这两个不同格式的书写块以两个块表示。现在我想做的是遍历所有块,并为每个块获取文本和字体。所以在这种情况下,第一次循环时,它会找到 "Hello" 和 " font-family: \".SFUIText-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt",第二次它会找到 "world" 并且它是 font
我可以使用代码循环浏览字体
text.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, text.length), options: NSAttributedStringEnumerationOptions()) { (font: AnyObject?, range: NSRange, usmp: UnsafeMutablePointer<ObjCBool>) -> Void in
print(font)
}
有没有办法对实际文本做同样的事情?
是 - 只需枚举所有属性而不是一个。
假设您有一个像这样的属性字符串:
// Create the string
let text = NSMutableAttributedString(string: "hello world")
let font = UIFont(name: ".SFUIText-Regular", size: 17)!
let boldFont = UIFont(name: ".SFUIText-Bold", size: 17)!
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, text.string.characters.count))
text.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, 6))
text.addAttribute(NSFontAttributeName, value: boldFont, range: NSMakeRange(6, 5))
您可以像这样创建类似于示例中的输出:
// Enumerate the attributes
text.enumerateAttributesInRange(NSMakeRange(0, text.string.characters.count), options: []) { (attribute, range, stop) -> Void in
let substring = (text.string as NSString).substringWithRange(range)
debugPrint(substring, attribute)
}
输出如下所示:
"hello " ["NSFont": <UICTFont: 0x7fba19724be0> font-family: ".SFUIText-Regular"; font-weight: normal; font-style: normal; font-size: 17.00pt, "NSColor": UIDeviceWhiteColorSpace 0 1]
"world" ["NSFont": <UICTFont: 0x7fba1960d8d0> font-family: ".SFUIText-Bold"; font-weight: bold; font-style: normal; font-size: 17.00pt, "NSColor": UIDeviceWhiteColorSpace 0 1]