David Thielen
David Thielen

Reputation: 32874

How do I write text to Graphics2D with justified alignment?

Using AttributedString and addAttribute() I can write out a line of formatted text, with the formatting set to sub-ranges of the text.

Using LineBreakMeasurer.nextLayout().getAdvance() I can then both center & right align. (And left align is trivial.)

But how do I tell it to justify it to a left and right margin? I think it's to use TextAttribute.JUSTIFICATION but it's not clear how to use it. It says a range of 0..1 and 1 clearly is no impact, but it's not clear how other settings will cause a spacing out of the text.

Or... am I going about this all wrong? I need to write lines of text (not paragraphs - I handle the line breaks) where the lines of text can have different formatting over sub-ranges of the text. And the line can be left, center, right, or justify aligned.

Upvotes: 1

Views: 781

Answers (1)

David Thielen
David Thielen

Reputation: 32874

The trick is to use TextLayout.getJustifiedLayout(). That does it - no need for TextAttribute.JUSTIFICATION.

Upvotes: 1

Related Questions