Added in API level 29

LineBreaker


open class LineBreaker
kotlin.Any
   ↳ android.graphics.text.LineBreaker

Provides automatic line breaking for a single paragraph.

<code>
  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 &lt; 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 &lt; 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);
  }
  </code>

Summary

Nested classes

Helper class for creating a LineBreaker.

open

Line breaking constraints for single paragraph.

open

Holds the result of the line breaking algorithm.

Constants
static Int

Value for break strategy indicating balanced line breaking.

static Int

Value for break strategy indicating high quality line breaking.

static Int

Value for break strategy indicating simple line breaking.

static Int

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

static Int

Value for hyphenation frequency indicating no automatic hyphenation.

static Int

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

static Int

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

static Int

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

static Int

Value for justification mode indicating no justification.

Public methods
open LineBreaker.Result
computeLineBreaks(measuredPara: MeasuredText, constraints: LineBreaker.ParagraphConstraints, lineNumber: Int)

Break paragraph into lines.

Constants

BREAK_STRATEGY_BALANCED

Added in API level 29
static val BREAK_STRATEGY_BALANCED: Int

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.

Value: 2

BREAK_STRATEGY_HIGH_QUALITY

Added in API level 29
static val BREAK_STRATEGY_HIGH_QUALITY: Int

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.

Value: 1

BREAK_STRATEGY_SIMPLE

Added in API level 29
static val BREAK_STRATEGY_SIMPLE: Int

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.

Value: 0

HYPHENATION_FREQUENCY_FULL

Added in API level 29
static val HYPHENATION_FREQUENCY_FULL: Int

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.

Value: 2

HYPHENATION_FREQUENCY_NONE

Added in API level 29
static val HYPHENATION_FREQUENCY_NONE: Int

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.

Value: 0

HYPHENATION_FREQUENCY_NORMAL

Added in API level 29
static val HYPHENATION_FREQUENCY_NORMAL: Int

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.

Value: 1

JUSTIFICATION_MODE_INTER_CHARACTER

Added in API level 35
static val JUSTIFICATION_MODE_INTER_CHARACTER: Int

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

Value: 2

JUSTIFICATION_MODE_INTER_WORD

Added in API level 29
static val JUSTIFICATION_MODE_INTER_WORD: Int

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

Value: 1

JUSTIFICATION_MODE_NONE

Added in API level 29
static val JUSTIFICATION_MODE_NONE: Int

Value for justification mode indicating no justification.

Value: 0

Public methods

computeLineBreaks

Added in API level 29
open fun computeLineBreaks(
    measuredPara: MeasuredText,
    constraints: LineBreaker.ParagraphConstraints,
    lineNumber: Int
): LineBreaker.Result

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
Return
LineBreaker.Result This value cannot be null.