简体   繁体   中英

How can I replace specific characters in UITextView

Hi in the log window I have two array.First one is my colored one as you can see the simulator blue ones.I want to change/replace blue ones with the second array but they are my chords .

截屏

Actually I tried like in following codes but all blue text changed with "Dbm" that was the second array's first string.

for txt in coloredTexts {

    for transposed in transposedAkorArray{
        
        self.akor_goster.text = self.akor_goster.text.replacingOccurrences(of: "\(txt)", with: "\(transposed)")
    }
    
}

Try,

for (index,txt) in coloredTexts.enumerated() {
    let range = akor_goster.text.range(of: txt)
    akor_goster.text.replacingOccurrences(of: txt, with: transposedAkorArray[index], options: [], range: range)
}

I think you are trying to do something like this:

for i in 0..<coloredTexts.count {
    self.akor_goster.text = self.akor_goster.text.replacingOccurrences(of: "\(coloredTexts[i])", with: "\(transposedAkorArray[i])")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM