nsregularexpression - Caret of UITextView is Being Hidden After Updating Text with Attributed String -
i using 2 text views visualize result of regular expression search. search concludes with resulting matches being highlighted in cyan using nsattributedstring
background color set.
after deleting character in result, result no longer highlighted.
however, after re-entering deleted character, result regains highlighted background, caret hidden behind cyan background. may difficult see in following screenshot, can see caret extending above , below after e
.
the code handles highlighting not final code; needs refactored first try. code follows:
public override func viewdidload() { super.viewdidload() notificationcenter.default.addobserver(forname: .uitextviewtextdidchange, object: nil, queue: .main) { notification in let attributed = nsmutableattributedstring(string: self.subjecttextview.text) let matches = regularexpressionsearchresult.matches(in: self.subjecttextview.text, using: self.patterntextview.text) match in matches { attributed.addattribute(nsbackgroundcolorattributename, value: uicolor.cyan, range: match.range) } attributed.addattribute(nsfontattributename, value: self.patterntextview.font!, range: nsrange(location: 0, length: self.subjecttextview.text.characters.count)) let selectedsubjectrange = self.subjecttextview.selectedrange self.subjecttextview.attributedtext = attributed self.subjecttextview.selectedrange = selectedsubjectrange } }
what need prevent caret being presented behind background of result after result has been modified remove highlight , modified again replace highlight?
Comments
Post a Comment