LineBreaker

public class LineBreaker
extends Object

java.lang.Object
   ↳ android.graphics.text.LineBreaker


Provides automatic line breaking for a single paragraph.

 
 Paint paint = new Paint();
 Paint bigPaint = new Paint();
 bigPaint.setTextSize(paint.getTextSize() * 2.0);
 String text = "Hello, Android.";

 // Prepare the measured text
 MeasuredText mt = new MeasuredText.Builder(text.toCharArray())
     .appendStyleRun(paint, 7, false)  // Use paint for "Hello, "
     .appednStyleRun(bigPaint, 8, false)  // Use bigPaint for "Hello, "
     .build();

 LineBreaker lb = new LineBreaker.Builder()
     // Use simple line breaker
     .setBreakStrategy(LineBreaker.BREAK_STRATEGY_SIMPLE)
     // Do not add hyphenation.
     .setHyphenationFrequency(LineBreaker.HYPHENATION_FREQUENCY_NONE)
     // Build the LineBreaker
     .build();

 ParagraphConstraints c = new ParagraphConstraints();
 c.setWidth(240);  // Set the line wieth as 1024px

 // Do the line breaking
 Result r = lb.computeLineBreaks(mt, c, 0);

 // Compute the total height of the text.
 float totalHeight = 0;
 for (int i = 0; i < r.getLineCount(); ++i) {  // iterate over the lines
    totalHeight += r.getLineDescent(i) - r.getLineAscent(i);
 }

 // Draw text to the canvas
 Bitmap bmp = Bitmap.createBitmap(240, totalHeight, Bitmap.Config.ARGB_8888);
 Canvas c = new Canvas(bmp);
 float yOffset = 0f;
 int prevOffset = 0;
 for (int i = 0; i < r.getLineCount(); ++i) {  // iterate over the lines
     int nextOffset = r.getLineBreakOffset(i);
     c.drawText(text, prevOffset, nextOffset, 0f, yOffset, paint);

     prevOffset = nextOffset;
     yOffset += r.getLineDescent(i) - r.getLineAscent(i);
 }
 
 

Summary

Nested classes

class LineBreaker.Builder

Helper class for creating a LineBreaker

class LineBreaker.ParagraphConstraints

Line breaking constraints for single paragraph. 

class LineBreaker.Result

Holds the result of the line breaking algorithm

Constants

int BREAK_STRATEGY_BALANCED

Value for break strategy indicating balanced line breaking.

int BREAK_STRATEGY_HIGH_QUALITY

Value for break strategy indicating high quality line breaking.

int BREAK_STRATEGY_SIMPLE

Value for break strategy indicating simple line breaking.

int HYPHENATION_FREQUENCY_FULL

Value for hyphenation frequency indicating the full amount of automatic hyphenation.

int HYPHENATION_FREQUENCY_NONE

Value for hyphenation frequency indicating no automatic hyphenation.

int HYPHENATION_FREQUENCY_NORMAL

Value for hyphenation frequency indicating a light amount of automatic hyphenation.

int JUSTIFICATION_MODE_INTER_WORD

Value for justification mode indicating the text is justified by stretching word spacing.

int JUSTIFICATION_MODE_NONE

Value for justification mode indicating no justification.

Public methods

LineBreaker.Result computeLineBreaks(MeasuredText measuredPara, LineBreaker.ParagraphConstraints constraints, int lineNumber)

Break paragraph into lines.

Inherited methods

Constants

BREAK_STRATEGY_BALANCED

Added in API level 29
public static final int BREAK_STRATEGY_BALANCED

Value for break strategy indicating balanced line breaking. The line breaker does whole-paragraph optimization for making all lines similar length, and also applies automatic hyphenation when required. This break strategy is good for small screen devices such as watch screens.

Constant Value: 2 (0x00000002)

BREAK_STRATEGY_HIGH_QUALITY

Added in API level 29
public static final int BREAK_STRATEGY_HIGH_QUALITY

Value for break strategy indicating high quality line breaking. With this option line breaker does whole-paragraph optimization for more readable text, and also applies automatic hyphenation when required.

Constant Value: 1 (0x00000001)

BREAK_STRATEGY_SIMPLE

Added in API level 29
public static final int BREAK_STRATEGY_SIMPLE

Value for break strategy indicating simple line breaking. The line breaker puts words to the line as much as possible and breaks line if no more words can fit into the same line. Automatic hyphens are only added when a line has a single word and that word is longer than line width. This is the fastest break strategy and ideal for editor.

Constant Value: 0 (0x00000000)

HYPHENATION_FREQUENCY_FULL

Added in API level 29
public static final int HYPHENATION_FREQUENCY_FULL

Value for hyphenation frequency indicating the full amount of automatic hyphenation. This hyphenation frequency is useful for running text and where it's important to put the maximum amount of text in a screen with limited space.

Constant Value: 2 (0x00000002)

HYPHENATION_FREQUENCY_NONE

Added in API level 29
public static final int HYPHENATION_FREQUENCY_NONE

Value for hyphenation frequency indicating no automatic hyphenation. Using this option disables auto hyphenation which results in better text layout performance. A word may be broken without hyphens when a line has a single word and that word is longer than line width. Soft hyphens are ignored and will not be used as suggestions for potential line breaks.

Constant Value: 0 (0x00000000)

HYPHENATION_FREQUENCY_NORMAL

Added in API level 29
public static final int HYPHENATION_FREQUENCY_NORMAL

Value for hyphenation frequency indicating a light amount of automatic hyphenation. This hyphenation frequency is useful for informal cases, such as short sentences or chat messages.

Constant Value: 1 (0x00000001)

JUSTIFICATION_MODE_INTER_WORD

Added in API level 29
public static final int JUSTIFICATION_MODE_INTER_WORD

Value for justification mode indicating the text is justified by stretching word spacing.

Constant Value: 1 (0x00000001)

JUSTIFICATION_MODE_NONE

Added in API level 29
public static final int JUSTIFICATION_MODE_NONE

Value for justification mode indicating no justification.

Constant Value: 0 (0x00000000)

Public methods

computeLineBreaks

Added in API level 29
public LineBreaker.Result computeLineBreaks (MeasuredText measuredPara, 
                LineBreaker.ParagraphConstraints constraints, 
                int lineNumber)

Break paragraph into lines. The result is filled to out param.

Parameters
measuredPara MeasuredText: a result of the text measurement This value cannot be null.

constraints LineBreaker.ParagraphConstraints: for a single paragraph This value cannot be null.

lineNumber int: a line number of this paragraph Value is 0 or greater

Returns
LineBreaker.Result This value cannot be null.