Change the color of individual characters in a glyph

As I understand it, a string contains glyphs, and a glyph can be composed of individual characters. This is a problem for me, since I would like to change the color of some diacritics in the line.

Let's say we have the following line:

วาีม

For this line, I would like to make the consonants a different color, since it is diacritic. Ie: I want a different color for วาม

and

.

From my tests it seems that I can only tag individual glyphs. I can't seem to change the color at the characteristic (diacritical) level. Sample 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 look like this:

enter image description here

As you can see, the diacritic tint is not displayed in green.

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

+3


source to share


2 answers


I doubt you will find a good way to do this. The fretboard for าี

in a font is very different from the glyph for

. For example, in the Thonburi font on my Mac, the first is glyph 1507 and the last is character 78. Each glyph entry in the font is a completely separate little description of how to draw the glyph. For a combined glyph, the diacritic is not a separate thing. The system has no way of knowing when it is drawing the base character and when it is drawing the diacritic. He just draws one. Thus, he cannot apply different colors.



I'm completely unfamiliar with Thai, so I'm just pondering this next part, although this is certainly true for some languages: I suspect there are glyphs for combining sequences that are radically different from what you would get by simply superimposing constituent parts on top of each other. friend. So, even in principle, it is unclear if a visually shared diacritic form exists against the base character.

+2


source


An alternative is to overlap two labels; one of which contains the diacritic text in the color you want for the diacritic; one on top of it with the same text, but no diacritics, in the color you want for the text.



When rendering, identical bits of text should undo exactly, leaving you with text and diacritics in the correct colors.

+1


source







All Articles