lixiang1994 / AttributedString

基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ASAttributedString如何通过range截取部分富文本?

zhangxiongwen opened this issue · comments

NSAttributedString 有这个方法 attributedString .attributedSubstring(from: range)
ASAttributedString应该如何通过range获取部分富文本呢?

commented

ASAttributedString有value属性 可以取到NSAttributedString

比如说 let str = ASAttributedString()
contentLab.attributed.text = str
我现在要截取str的一部分显示,如果我用ASAttributedString有value属性获取到的值是NSAttributedString类型,但是我要给contentLab.attributed.text 赋值, 这个是ASAttributedString类型的,要怎么写呢?

commented

contentLab.attributed.text = .init(str.value.attributedSubstring(from: range))

commented

如果可以 我还是建议分开创建ASAttributedString, 没必要对ASAttributedString进行截取

commented

是什么场景需要你做截取?

我要在末尾加上展开折叠,然后在计算出range后截取 ,在末尾追加展开折叠

commented

哈哈 好吧 那确实需要截取

contentLab.attributed.text = .init(str.value.attributedSubstring(from: range))

这样写会不会对已经添加的属性 比如点击事件或者其他的属性有影响呢?

commented

据我对.attributedSubstring(from: range)的了解, 如果刚好截取到某点击事件的部分, 截取出的那段依旧会有action, 理论上不会有影响, 你可以测试一下.

好 感谢大佬