Question

Is there a simple way to draw underlined text in draw2d without manually drawing a line under the text?

Was it helpful?

Solution

After a bit of research, it looks like underlined text isn't supported natively in Draw2D since SWT fonts are OS level objects and not every OS supported by SWT supports underlined text. Looks like the best bet is to create a method that draws underlined text manually. Maybe it's something that will get added into SWT later.

Here's what I ended up using (more or less):

private void drawUnderlinedText(GC gc, String string, int x, int y)
{
    Point extent = gc.textExtent(string);
    gc.drawText(string, x, y);
    gc.drawLine(x - 1, y + extent.y - 1, x + extent.x - 1, y + extent.y - 1);
}

OTHER TIPS

Use a Font with an underline attribute. Darryl's Visual Font Designer shows how to create a font with this property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top