我有一个NSAttributedString,我想在每个“***“上拆分它。所以实际上,我想在这个字符串出现的地方拆分一个NSAttributedString。结果应该类似于NSAttributedStrings数组。谢谢你的帮忙!
发布于 2019-05-06 22:51:43
您可以将此扩展用于NSAttributedString
private extension NSAttributedString {
func components(separatedBy separator: String) -> [NSAttributedString] {
var result = [NSAttributedString]()
let separatedStrings = string.components(separatedBy: separator)
var range = NSRange(location: 0, length: 0)
for string in separatedStrings {
range.length = string.count
let attributedString = attributedSubstring(from: range)
result.append(attributedString)
range.location += range.length + separator.count
}
return result
}
}示例`
let atributedString: NSAttributedString = NSAttributedString(string: "A***B***C***D")
let resultArray = atributedString.components(separatedBy: "***")
for atString in resultArray {
print(atString)
}https://stackoverflow.com/questions/56007455
复制相似问题