Вы можете использовать это расширение для 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)
}