ios - Replace regex match with attributed string and text -
our app api returns field custom format user mentions like: "this text mention @(steve|user_id) " . before display on uitextview , need process text, find pattern , replace more user friendly. final result "this text mention @steve" @steve should have link attribute user_id . same functionality facebook. first i've created uitextview extension, match function regex pattern. extension uitextview { func processtext(pattern: string) { let instring = self.text let regex = try? nsregularexpression(pattern: pattern, options: []) let range = nsmakerange(0, instring.characters.count) let matches = (regex?.matchesinstring(instring, options: [], range: range))! [nstextcheckingresult] let attrstring = nsmutableattributedstring(string: instring, attributes:attrs) //iterate on regex matches match in matches { //properly print match range print(match.range) //a basic id...