如果我使用
从文本字段中获取属性字符串let text = input.attributedText!
print(text)
在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 “第二次它找到了”世界“和它的字体
我可以使用代码
循环遍历字体text.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, text.length), options: NSAttributedStringEnumerationOptions()) { (font: AnyObject?, range: NSRange, usmp: UnsafeMutablePointer<ObjCBool>) -> Void in
print(font)
}
有没有办法对实际文本做同样的事情?
答案 0 :(得分:1)
是 - 只需枚举所有属性,而不只是一个。
假设你有一个像这样的属性字符串:
// 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]