swift - 如何将 NSAttributedString 拆分为 NSAttributedString 数组
问题描述
解决方案
You can use this extension for 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
}
}
Example`
let atributedString: NSAttributedString = NSAttributedString(string: "A***B***C***D")
let resultArray = atributedString.components(separatedBy: "***")
for atString in resultArray {
print(atString)
}
推荐阅读
- java - Java 编译器会在编译期间计算常量表达式吗?
- arrays - 在 loadrunner 中传递 c 数组
- java - 如何摆脱这个错误'将 findViewById 转换为 EditText 是多余的'?
- jquery - Bootstrap 4 DateTimePicker Multidate 不工作
- sql - 在 Scala 上,如何使用 sql 语句将参数传递给文件并使用 sqlContext.sql?
- ios - React Native api 调用不会在远程调试模式之外发生(仅限 iOS)
- android - 如何在 Android 片段中使用 TakuSemba 的 MultiSnapRecyclerView?
- java - 在一行定义变量并在下一行创建它的实例有什么好处
- javascript - codemirror:搜索并突出显示多个单词而无需对话框
- java - java中锁的工作原理