# How to draw text underline in colour?

**URL:** <https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208>\
**Category:** General JUCE discussion\
**Created:** [March 28, 2013, 10:31pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208 "2013-03-28T22:31:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugh](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hugh](https://forum.juce.com/u/hugh)\
**Post date:** [March 28, 2013, 10:31pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/1 "2013-03-28T22:31:44Z")

</div>

i want to draw colour underlines to text (ie in a different colour to the text).

I’m using the `GlyphArrangement' directly, but these is no way i can find. Also`Graphics’ is const around here…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 29, 2013, 8:58am UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/2 "2013-03-29T08:58:33Z")

</div>

Not possible as things currently stand! You might be able to hack together your own version of GlyphArrangement::drawGlyphUnderline though?

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [March 29, 2013, 1:42pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/3 "2013-03-29T13:42:16Z")

</div>

If it’s just a small piece of text you could first draw the text underlined with your underline color and then another time not underlined with your normal text color.  
Could work.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 29, 2013, 2:46pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/4 "2013-03-29T14:46:28Z")

</div>

[quote=“gekkie100”]If it’s just a small piece of text you could first draw the text underlined with your underline color and then another time not underlined with your normal text color.  
Could work.[/quote]

No - you’d get some nasty anti-aliasing artifacts if you did that.

---

<div class="post-metadata">

**Author:** ![hugh](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hugh](https://forum.juce.com/u/hugh)\
**Post date:** [March 29, 2013, 4:19pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/5 "2013-03-29T16:19:34Z")

</div>

No problem. i’m quite happy to make my own local changes.

currently, im doing this, which is qute horrible because i const cast away and change the state.

[code]inline void GlyphArrangement::drawGlyphUnderline (const Graphics& g, const PositionedGlyph& pg,  
const int i, const AffineTransform& transform, const Colour& ulCol) const  
{

…  
Path p;  
p.addRectangle (pg.x, pg.y + lineThickness \* 2.0f, nextX - pg.x, lineThickness);

```
// XXX HACK
Graphics& gm = const_cast<Graphics&>(g);
gm.saveState();
gm.setColour(ulCol);
g.fillPath (p, transform);
gm.restoreState();

```

}[/code]

I’ve had to do a `saveState' and`restoreState’ here because there is no `getColour' on`Graphics’ otherwise i could get the old colour, change it, use it and set it back.

I’m hoping there’s not too much overhead here. is there a better way without too much juce changing. I prefer to keep my juce changes as local as possible.

thanks,

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 29, 2013, 5:19pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/6 "2013-03-29T17:19:58Z")

</div>

Personally, I’d probably leave the juce code alone, and draw a non-underlined GlyphArrangement, and then write my own external version of drawGlyphUnderline that iterates the glyph arrangement to draw the lines.

---

<div class="post-metadata">

**Author:** ![hugh](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@hugh](https://forum.juce.com/u/hugh)\
**Post date:** [March 29, 2013, 9:56pm UTC](https://forum.juce.com/t/how-to-draw-text-underline-in-colour/10208/7 "2013-03-29T21:56:20Z")

</div>

thank you!

I hadn’t noticed you can get all the information out of a GlyphArrangement and a `PositionedGlyph’ but you can. This is by far the best way. I’ve reverted my hacks and written my own draw for each glyph.

Also, later on i want to draw different kinds of underlines not just different colours. so this fixes that too.

– hugh.
