2015-09-27 48 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

謝謝,這是有效的。 –