Reputation: 3246
I'm trying to replace some text that was selected inside a UITextView
with some NSAttributedString
but only the following method is available:
textView.replace(UITextRange, withText: String)
As you can see, replacing text only accepts a String
and I cannot find way to replace it with an NSAttributedString
.
One thing I could do is to store the whole UITextView attributedText
and then perform the desired changes on a NSMutableAttributedString
and then I can replace the UITextView.attributedText
to be the one of the NSMutableAttributedString
, but this comes with some issues for me.
If the text is already long with some NSStorage
and NSAttachments
this will be way more expensive.
Is there any workaround?
Upvotes: 3
Views: 591
Reputation: 72855
Because you can't intermix String
and NSAttributedString
, there's unfortunately no workaround that will let both co-exist in a text field.
But you should be able to use replaceCharacters(in:with:)
:
existingAttributedString.replaceCharacters(in: range, with: replacementAttributedString)
Upvotes: 2