Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从NSAttributedString获取文本块_Ios_Swift2_Nsattributedstring - Fatal编程技术网

Ios 从NSAttributedString获取文本块

Ios 从NSAttributedString获取文本块,ios,swift2,nsattributedstring,Ios,Swift2,Nsattributedstring,如果我使用 let text = input.attributedText! print(text) 在Swift中,则输出如下(当输入包含常规的“hello”,然后是粗体的“world”) 你好{ NSColor=“UIDeviceWhiteColorSpace 0 1”; NSFont=“font-family:\”.SFUIText-Regular\“字体大小:正常;字体样式:正常;字体大小:17.00pt”; NSParagraphStyle=“对齐4、行距0、段落间距0、段落间距0、

如果我使用

let text = input.attributedText!
print(text)
在Swift中,则输出如下(当输入包含常规的“hello”,然后是粗体的“world”)

你好{ NSColor=“UIDeviceWhiteColorSpace 0 1”; NSFont=“font-family:\”.SFUIText-Regular\“字体大小:正常;字体样式:正常;字体大小:17.00pt”; NSParagraphStyle=“对齐4、行距0、段落间距0、段落间距0、段落间距0、首缩进0、尾缩进0、首行首缩进0、线宽0/0、线宽倍数0、换行模式2、制表符(\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,Block(\n),List(\n),BaseWritingDirection 0,连字符系数0,TighteningFortRunning NO,HeaderLevel 0”; NSShadow=“NSShadow{0,-1}color={(null)}”; } 世界{ NSColor=“UIDeviceWhiteColorSpace 0 1”; NSFont=“font-family:\”.SFUIText Bold\“字体大小:粗体;字体样式:普通;字体大小:17.00pt”; NSParagraphStyle=“对齐4、行距0、段落间距0、段落间距0、段落间距0、首缩进0、尾缩进0、首行首缩进0、线宽0/0、线宽倍数0、换行模式2、制表符(\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,Block(\n),List(\n),BaseWritingDirection 0,连字符系数0,TighteningFortRunning NO,HeaderLevel 0”; NSShadow=“NSShadow{0,-1}color={(null)}”; } 我可以看到,当打印到控制台时,两个不同格式的书写块在两个块中表示。现在我要做的是循环遍历所有的块,对于每个块,得到文本和字体。因此,在本例中,第一次循环时,它会找到“Hello”和“font family:\”.SFUIText Regular\“字体重量:正常;字体样式:正常;字体大小:17.00pt”,第二次它会找到“world”和它的字体

我可以使用代码循环浏览字体

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

        print(font)
    }
text.EnumerateAttributeName(NSFontAttributeName,inRange:NSMakeRange(0,text.length),选项:NSAttributedStringEnumerationOptions()){(font:AnyObject?,range:NSRange,usmp:UnassemutablePointer)->中的Void
打印(字体)
}

有没有办法对实际文本执行相同的操作?

是-只需列举所有属性,而不是一个属性

假设您有如下属性字符串:

// 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]
您可以创建类似于示例中的输出,如下所示:

// 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]
输出如下所示:

// 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]
“hello”[“NSFont”:字体系列:“.SFUIText Regular”;字体重量:正常;字体样式:正常;字体大小:17.00pt,“NSColor”:UIDeviceWhiteColorSpace 0 1]
“世界”[“NSFont”:字体系列:“.SFUIText粗体”;字体大小:粗体;字体样式:普通;字体大小:17.00pt,“NSColor”:UIDeviceWhiteColorSpace 0 1]