ios - how can I change style of pre-selected words in my textView? -


this follow question how can change style of words in uitextview 1 one in swift?

thanks @josh's able write piece of code highlights each word begins # - , 1 one. final code was:

func highlight (to index: int) {      let regex = try? nsregularexpression(pattern: "#(\\w+)", options: [])     let matches = regex!.matches(in: hashtagexplanationtextview.text, options: [], range: nsmakerange(0, (hashtagexplanationtextview.text.characters.count)))     let titledict: nsdictionary = [nsforegroundcolorattributename: orangecolor]     let titledict2: nsdictionary = [nsforegroundcolorattributename: uicolor.red]     let storedattributedstring = nsmutableattributedstring(string: hashtagexplanationtextview.text!, attributes: titledict as! [string : anyobject])       let attributedstring = nsmutableattributedstring(attributedstring: storedattributedstring)     guard index < matches.count else {         return     }      in 0..<index{         let matchrange = matches[i].rangeat(0)         attributedstring.addattributes(titledict2 as! [string : anyobject], range: matchrange)     }     hashtagexplanationtextview.attributedtext = attributedstring     if #available(ios 10.0, *) {         let _ = timer.scheduledtimer(withtimeinterval: 1, repeats: false) { _ in             self.highlight(to: index + 1)         }     } else {         dispatchqueue.main.asyncafter(deadline: .now() + 1) {             self.highlight(to: index + 1)         }     } } 

this works fine, change logic not highlight # words, highlights (one one) words preselected array of words.

so have array var myarray:[string] = ["those","words","are","highlighted"] , how can put instead of regex match in code?

i believe using regex array of nsrange. here, need different datastructure [string : [nsrange]]. can use rangeofstring function detect nsrange word located. can follow example given below that:

let wordmatcharray:[string] = ["those", "words", "are", "highlighted"] let labeltext:nsstring = nsstring(string: "those words, ldsnvldnvsdnds, are, highlighted,words highlighted") let textlength:int = labeltext.length  var dictionaryforeachword:[string : [nsrange]] = [:]  eachword:string in wordmatcharray {     var prevrange:nsrange = nsmakerange(0, 0)    var rangearray:[nsrange] = []     while ((prevrange.location + prevrange.length) < textlength) {        let start:int = (prevrange.location + prevrange.length)       let rangeeach:nsrange = labeltext.range(of: eachword, options: nsstring.compareoptions.literal, range: nsmakerange(start, textlength-start))       if rangeeach.length == 0 {          break       }       rangearray.append(rangeeach)       prevrange = rangeeach    }     dictionaryforeachword[eachword] = rangearray } 

now have array of nsrange i.e, [nsrange] each word stored in dictionary, can highlight each word accordingly in uitextview.

feel free comment if have doubts regarding implementation :)


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -