UITextView,插入 attributeString 后,怎么恢复样式?

问题描述

在 textView 用代码 append 一个 attributeString 后,继续键盘编辑,样式不能恢复

图片描述

相关代码

button 的代码如下:

let attributeText = NSMutableAttributedString(attributedString: textView.attributedText)
attributeText.append(NSAttributedString(string: "@黄小小", attributes: [NSAttributedString.Key.foregroundColor: UIColor.cyan]))
self.textView.attributedText = attributeText

你期待的结果是什么?实际看到的错误信息又是什么?

希望 textView 重新编辑的时候恢复成开始的样式

阅读 2.6k
1 个回答

找到答案了,可以在 shouldChangeTextIn 里设置 textView 的 typingAttributes 重设成之前的样式

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题