Adding an Add-on to NSMutableAttributedString in a UITextView

How to add padding to a substring UITextViewthat is NSMutableAttributedString(shown here highlighted in yellow)?

enter image description here

Currently, I have attribute text (image 2) without any additions around the attribute text, but you need to add some addition (image 3) to it, which represents some distance around the selected text (not all UITextView.

Here is a close-up comparing how NSMutableAttributedStringwith / without filling out (this addition should not apply to the rest of the text that is not allowed):

enter image description here

This is how I get the text from now UITextView(after finding the range of text I want to select and save the attributed text in attributedText):

var highlightedText = NSMutableAttributedString(string: "this text is highlighted")
let highlightedRange = NSRange(location: 0, length: highlightedText.characters.count)

highlightedText.addAttribute(NSBackgroundColorAttributeName,
                             value: UIColor.yellow, range: highlightedRange)

attributedText.replaceCharacters(in: highlightedRange, with: highlightedText)
+4

Source: https://habr.com/ru/post/1665068/


All Articles