Search code examples
javatextdrawingawt

Handling \n in LineBreakMeasurer


There are thousand articles how to use LineBreakMeasurer to draw multi-line text but there is none about drawing multi-line text taking into account also \n(when you want to force a new line at a specific position in text and not only when the right - or left - margin ends).

The secret seems to lie in BreakIterator, but I couldn't find an implementation which handles \n.


Solution

  • Tokenize the text first, then just apply the LineBreakMeasureCode to each token.