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)
}
有沒有辦法做同樣的實際文本字體循環?
謝謝,這是有效的。 –