2015-09-27 47 views
1

获取文本块,如果我得到一个文本字段使用从NSAttributedString

let text = input.attributedText! 
print(text) 

斯威夫特,则输出如下(当输入中包含“你好”定期然后“世界”的属性串以粗体显示)

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:\”。 “而第二次,就会发现‘世界’和它的字体

我可以通过使用代码

text.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, text.length), options: NSAttributedStringEnumerationOptions()) { (font: AnyObject?, range: NSRange, usmp: UnsafeMutablePointer<ObjCBool>) -> Void in 

     print(font) 
    } 

有没有办法做同样的实际文本字体循环?

回答

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] 
+0

谢谢,这是有效的。 –