Class StaticLayout.Builder

java.lang.Object
icyllis.modernui.text.StaticLayout.Builder
Enclosing class:
StaticLayout

public static final class StaticLayout.Builder extends Object
Builder for static layouts. The builder is the preferred pattern for constructing StaticLayout objects and should be preferred over the constructors, particularly to access newer features. To build a static layout, first call StaticLayout.builder(CharSequence, int, int, TextPaint, int) with the required arguments (text, paint, and width), then call setters for optional parameters, and finally build() to build the StaticLayout object. Parameters not explicitly set will get default values.
  • Method Details

    • setAlignment

      @NonNull public StaticLayout.Builder setAlignment(@NonNull Layout.Alignment alignment)
      Set the alignment. The default is Layout.Alignment.ALIGN_NORMAL.
      Parameters:
      alignment - Alignment for the resulting StaticLayout
      Returns:
      this builder, useful for chaining
    • setTextDirection

      @NonNull public StaticLayout.Builder setTextDirection(@NonNull TextDirectionHeuristic textDir)
      Set the text direction heuristic. The text direction heuristic is used to resolve text direction per-paragraph based on the input text. The default is TextDirectionHeuristics.FIRSTSTRONG_LTR.
      Parameters:
      textDir - text direction heuristic for resolving bidi behavior.
      Returns:
      this builder, useful for chaining
    • setIncludePad

      @NonNull public StaticLayout.Builder setIncludePad(boolean includePad)
      Set whether to include extra space beyond font ascent and descent (which is needed to avoid clipping in some languages, such as Arabic and Kannada). The default is true.
      Parameters:
      includePad - whether to include padding
      Returns:
      this builder, useful for chaining
    • setFallbackLineSpacing

      @NonNull public StaticLayout.Builder setFallbackLineSpacing(boolean fallbackLineSpacing)
      Set whether to respect the ascent and descent of the fallback fonts that are used in displaying the text (which is needed to avoid text from consecutive lines running into each other). If set, fallback fonts that end up getting used can increase the ascent and descent of the lines that they are used on.

      The default is true. It is required to be true if text could be in languages like Burmese or Tibetan where text is typically much taller or deeper than Latin text.

      Parameters:
      fallbackLineSpacing - whether to expand line spacing based on fallback fonts
      Returns:
      this builder, useful for chaining
    • setEllipsizedWidth

      @NonNull public StaticLayout.Builder setEllipsizedWidth(int ellipsizedWidth)
      Set the width as used for ellipsizing purposes, if it differs from the normal layout width. The default is the width passed to build().
      Parameters:
      ellipsizedWidth - width used for ellipsizing, in pixels
      Returns:
      this builder, useful for chaining
    • setEllipsize

      Set ellipsizing on the layout. Causes words that are longer than the view is wide, or exceeding the number of lines (see #setMaxLines) in the case of TextUtils.TruncateAt.END or TextUtils.TruncateAt.MARQUEE, to be ellipsized instead of broken. The default is null, indicating no ellipsis is to be applied.
      Parameters:
      ellipsize - type of ellipsis behavior
      Returns:
      this builder, useful for chaining
    • setMaxLines

      @NonNull public StaticLayout.Builder setMaxLines(int maxLines)
      Set maximum number of lines. This is particularly useful in the case of ellipsizing, where it changes the layout of the last line. The default is unlimited.
      Parameters:
      maxLines - maximum number of lines in the layout
      Returns:
      this builder, useful for chaining
    • setIndents

      @NonNull public StaticLayout.Builder setIndents(@Nullable int[] leftIndents, @Nullable int[] rightIndents)
      Set indents. Arguments are arrays holding an indent amount, one per line, measured in pixels. For lines past the last element in the array, the last element repeats.
      Parameters:
      leftIndents - array of indent values for left margin, in pixels
      rightIndents - array of indent values for right margin, in pixels
      Returns:
      this builder, useful for chaining
    • setLineBreakConfig

      @NonNull public StaticLayout.Builder setLineBreakConfig(@NonNull LineBreakConfig lineBreakConfig)
      Set the line break configuration. The line break will be passed to native used for calculating the text wrapping. The default value of the line break style is LineBreakConfig.LINE_BREAK_STYLE_NONE
      Parameters:
      lineBreakConfig - the line break configuration for text wrapping.
      Returns:
      this builder, useful for chaining.
      See Also:
    • build

      @NonNull public StaticLayout build()
      Build the StaticLayout after options have been set.

      Note: the builder object must not be reused in any way after calling this method. Setting parameters after calling this method, or calling it a second time on the same builder object, will likely lead to unexpected results.

      Returns:
      the newly constructed StaticLayout object