Changing the color of individual characters in a glyph

Viewed 488

As I understand it, a string contains glyphs and a glyph might consist of individual characters. For me this is a problem, as I would like to change the color of some diacritics in a string.

Let's say we have the following string:

วาีม

For this string I would like to make the consonants a different color as it's diacritic. I.e.: I want a different color for วาม and .

From my tests it seems that I am only able to color individual glyphs. It seems I can not change the color at a character (diacritic) level. Some example code:

let text = NSMutableAttributedString(string: "วาีม")
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, 1))
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(1, 1))
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSMakeRange(2, 1))
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: NSMakeRange(3, 1))
label.attributedText = text

The above would render as follows:

enter image description here

As can be seen the diacritic is not rendered with a green color.

Does anyone know if there is some way to achieve the result I want?

2 Answers
Related