Builder
class Builder
kotlin.Any | |
↳ | android.text.StaticLayout.Builder |
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 obtain
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.
Summary
Public methods | |
---|---|
StaticLayout |
build() Build the |
static StaticLayout.Builder |
Obtain a builder for constructing StaticLayout objects. |
StaticLayout.Builder |
setAlignment(alignment: Layout.Alignment) Set the alignment. |
StaticLayout.Builder |
setBreakStrategy(breakStrategy: Int) Set break strategy, useful for selecting high quality or balanced paragraph layout options. |
StaticLayout.Builder |
setEllipsize(ellipsize: TextUtils.TruncateAt?) Set ellipsizing on the layout. |
StaticLayout.Builder |
setEllipsizedWidth(ellipsizedWidth: Int) Set the width as used for ellipsizing purposes, if it differs from the normal layout width. |
StaticLayout.Builder |
setHyphenationFrequency(hyphenationFrequency: Int) Set hyphenation frequency, to control the amount of automatic hyphenation used. |
StaticLayout.Builder |
setIncludePad(includePad: Boolean) 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). |
StaticLayout.Builder |
setIndents(leftIndents: IntArray?, rightIndents: IntArray?) Set indents. |
StaticLayout.Builder |
setJustificationMode(justificationMode: Int) Set paragraph justification mode. |
StaticLayout.Builder |
setLineBreakConfig(lineBreakConfig: LineBreakConfig) Set the line break configuration. |
StaticLayout.Builder |
setLineSpacing(spacingAdd: Float, spacingMult: Float) Set line spacing parameters. |
StaticLayout.Builder |
setMaxLines(maxLines: Int) Set maximum number of lines. |
StaticLayout.Builder |
setMinimumFontMetrics(minimumFontMetrics: Paint.FontMetrics?) Set the minimum font metrics used for line spacing. |
StaticLayout.Builder |
setShiftDrawingOffsetForStartOverhang(shiftDrawingOffsetForStartOverhang: Boolean) Set true for shifting the drawing x offset for showing overhang at the start position. |
StaticLayout.Builder! |
setText(source: CharSequence!) |
StaticLayout.Builder |
setTextDirection(textDir: TextDirectionHeuristic) Set the text direction heuristic. |
StaticLayout.Builder |
setUseBoundsForWidth(useBoundsForWidth: Boolean) Set true for using width of bounding box as a source of automatic line breaking and drawing. |
StaticLayout.Builder |
setUseLineSpacingFromFallbacks(useLineSpacingFromFallbacks: Boolean) 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). |
Public methods
build
fun build(): StaticLayout
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.
Return | |
---|---|
StaticLayout |
the newly constructed StaticLayout object This value cannot be null . |
obtain
static fun obtain(
source: CharSequence,
start: Int,
end: Int,
paint: TextPaint,
width: Int
): StaticLayout.Builder
Obtain a builder for constructing StaticLayout objects.
Parameters | |
---|---|
source |
CharSequence: The text to be laid out, optionally with spans This value cannot be null . |
start |
Int: The index of the start of the text Value is 0 or greater |
end |
Int: The index + 1 of the end of the text Value is 0 or greater |
paint |
TextPaint: The base paint used for layout This value cannot be null . |
width |
Int: The width in pixels Value is 0 or greater |
Return | |
---|---|
StaticLayout.Builder |
a builder object used for constructing the StaticLayout This value cannot be null . |
setAlignment
fun setAlignment(alignment: Layout.Alignment): StaticLayout.Builder
Set the alignment. The default is Layout.Alignment.ALIGN_NORMAL
.
Parameters | |
---|---|
alignment |
Layout.Alignment: Alignment for the resulting StaticLayout This value cannot be null . |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setBreakStrategy
fun setBreakStrategy(breakStrategy: Int): StaticLayout.Builder
Set break strategy, useful for selecting high quality or balanced paragraph layout options. The default is Layout.BREAK_STRATEGY_SIMPLE
.
Layout.HYPHENATION_FREQUENCY_NORMAL
or Layout.HYPHENATION_FREQUENCY_FULL
while line breaking is set to one of Layout.BREAK_STRATEGY_BALANCED
, Layout.BREAK_STRATEGY_HIGH_QUALITY
improves the structure of text layout however has performance impact and requires more time to do the text layout.
Parameters | |
---|---|
breakStrategy |
Int: break strategy for paragraph layout Value is android.graphics.text.LineBreaker#BREAK_STRATEGY_SIMPLE , android.graphics.text.LineBreaker#BREAK_STRATEGY_HIGH_QUALITY , or android.graphics.text.LineBreaker#BREAK_STRATEGY_BALANCED |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setEllipsize
fun setEllipsize(ellipsize: TextUtils.TruncateAt?): StaticLayout.Builder
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 android.text.TextUtils.TruncateAt#END
or android.text.TextUtils.TruncateAt#MARQUEE
, to be ellipsized instead of broken. The default is null
, indicating no ellipsis is to be applied.
Parameters | |
---|---|
ellipsize |
TextUtils.TruncateAt?: type of ellipsis behavior This value may be null . |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining |
See Also
setEllipsizedWidth
fun setEllipsizedWidth(ellipsizedWidth: Int): StaticLayout.Builder
Set the width as used for ellipsizing purposes, if it differs from the normal layout width. The default is the width
passed to obtain
.
Parameters | |
---|---|
ellipsizedWidth |
Int: width used for ellipsizing, in pixels Value is 0 or greater |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
See Also
setHyphenationFrequency
fun setHyphenationFrequency(hyphenationFrequency: Int): StaticLayout.Builder
Set hyphenation frequency, to control the amount of automatic hyphenation used. The possible values are defined in Layout
, by constants named with the pattern HYPHENATION_FREQUENCY_*
. The default is Layout.HYPHENATION_FREQUENCY_NONE
.
Layout.HYPHENATION_FREQUENCY_NORMAL
or Layout.HYPHENATION_FREQUENCY_FULL
while line breaking is set to one of Layout.BREAK_STRATEGY_BALANCED
, Layout.BREAK_STRATEGY_HIGH_QUALITY
improves the structure of text layout however has performance impact and requires more time to do the text layout.
Parameters | |
---|---|
hyphenationFrequency |
Int: hyphenation frequency for the paragraph Value is android.text.Layout#HYPHENATION_FREQUENCY_NORMAL , android.text.Layout#HYPHENATION_FREQUENCY_NORMAL_FAST , android.text.Layout#HYPHENATION_FREQUENCY_FULL , android.text.Layout#HYPHENATION_FREQUENCY_FULL_FAST , or android.text.Layout#HYPHENATION_FREQUENCY_NONE |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setIncludePad
fun setIncludePad(includePad: Boolean): StaticLayout.Builder
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 |
Boolean: whether to include padding |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setIndents
fun setIndents(
leftIndents: IntArray?,
rightIndents: IntArray?
): StaticLayout.Builder
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 |
IntArray?: array of indent values for left margin, in pixels This value may be null . |
rightIndents |
IntArray?: array of indent values for right margin, in pixels This value may be null . |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setJustificationMode
fun setJustificationMode(justificationMode: Int): StaticLayout.Builder
Set paragraph justification mode. The default value is Layout.JUSTIFICATION_MODE_NONE
. If the last line is too short for justification, the last line will be displayed with the alignment set by setAlignment
. When Justification mode is JUSTIFICATION_MODE_INTER_WORD, wordSpacing on the given Paint
will be ignored. This behavior also affects Spans which change the wordSpacing.
Parameters | |
---|---|
justificationMode |
Int: justification mode for the paragraph. Value is android.graphics.text.LineBreaker#JUSTIFICATION_MODE_NONE , android.graphics.text.LineBreaker#JUSTIFICATION_MODE_INTER_WORD , or android.graphics.text.LineBreaker#JUSTIFICATION_MODE_INTER_CHARACTER |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining. This value cannot be null . |
setLineBreakConfig
fun setLineBreakConfig(lineBreakConfig: LineBreakConfig): StaticLayout.Builder
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 |
LineBreakConfig: the line break configuration for text wrapping. This value cannot be null . |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining. This value cannot be null . |
setLineSpacing
fun setLineSpacing(
spacingAdd: Float,
spacingMult: Float
): StaticLayout.Builder
Set line spacing parameters. Each line will have its line spacing multiplied by spacingMult
and then increased by spacingAdd
. The default is 0.0 for spacingAdd
and 1.0 for spacingMult
.
Parameters | |
---|---|
spacingAdd |
Float: the amount of line spacing addition |
spacingMult |
Float: the line spacing multiplier Value is 0.0 or greater |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setMaxLines
fun setMaxLines(maxLines: Int): StaticLayout.Builder
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 |
Int: maximum number of lines in the layout Value is 0 or greater |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
See Also
setMinimumFontMetrics
fun setMinimumFontMetrics(minimumFontMetrics: Paint.FontMetrics?): StaticLayout.Builder
Set the minimum font metrics used for line spacing.
null
is the default value. If null
is set or left as default, the font metrics obtained by Paint.getFontMetricsForLocale(Paint.FontMetrics)
is used.
The minimum meaning here is the minimum value of line spacing: maximum value of Paint.ascent()
, minimum value of Paint.descent()
.
By setting this value, each line will have minimum line spacing regardless of the text rendered. For example, usually Japanese script has larger vertical metrics than Latin script. By setting the metrics obtained by Paint.getFontMetricsForLocale(Paint.FontMetrics)
for Japanese or leave it null
if the Paint's locale is Japanese, the line spacing for Japanese is reserved if the text is an English text. If the vertical metrics of the text is larger than Japanese, for example Burmese, the bigger font metrics is used.
Parameters | |
---|---|
minimumFontMetrics |
Paint.FontMetrics?: A minimum font metrics. Passing null for using the value obtained by Paint.getFontMetricsForLocale(Paint.FontMetrics) |
See Also
android.widget.TextView#setMinimumFontMetrics(Paint.FontMetrics)
android.widget.TextView#getMinimumFontMetrics()
android.text.Layout#getMinimumFontMetrics()
android.text.Layout.Builder#setMinimumFontMetrics(Paint.FontMetrics)
android.text.DynamicLayout.Builder#setMinimumFontMetrics(Paint.FontMetrics)
setShiftDrawingOffsetForStartOverhang
fun setShiftDrawingOffsetForStartOverhang(shiftDrawingOffsetForStartOverhang: Boolean): StaticLayout.Builder
Set true for shifting the drawing x offset for showing overhang at the start position. This flag is ignored if the getUseBoundsForWidth()
is false. If this value is false, the Layout draws text from the zero even if there is a glyph stroke in a region where the x coordinate is negative. If this value is true, the Layout draws text with shifting the x coordinate of the drawing bounding box. This value is false by default.
Parameters | |
---|---|
shiftDrawingOffsetForStartOverhang |
Boolean: true for shifting the drawing offset for showing the stroke that is in the region where the x coordinate is negative. |
Return | |
---|---|
StaticLayout.Builder |
This value cannot be null . |
setTextDirection
fun setTextDirection(textDir: TextDirectionHeuristic): StaticLayout.Builder
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 |
TextDirectionHeuristic: text direction heuristic for resolving bidi behavior. This value cannot be null . |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |
setUseBoundsForWidth
fun setUseBoundsForWidth(useBoundsForWidth: Boolean): StaticLayout.Builder
Set true for using width of bounding box as a source of automatic line breaking and drawing. If this value is false, the Layout determines the drawing offset and automatic line breaking based on total advances. By setting true, use all joined glyph's bounding boxes as a source of text width. If the font has glyphs that have negative bearing X or its xMax is greater than advance, the glyph clipping can happen because the drawing area may be bigger. By setting this to true, the Layout will reserve more spaces for drawing.
Parameters | |
---|---|
useBoundsForWidth |
Boolean: True for using bounding box, false for advances. |
Return | |
---|---|
StaticLayout.Builder |
this builder instance This value cannot be null . |
setUseLineSpacingFromFallbacks
fun setUseLineSpacingFromFallbacks(useLineSpacingFromFallbacks: Boolean): StaticLayout.Builder
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.
For backward compatibility reasons, the default is false
, but setting this to true is strongly recommended. 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 | |
---|---|
useLineSpacingFromFallbacks |
Boolean: whether to expand linespacing based on fallback fonts |
Return | |
---|---|
StaticLayout.Builder |
this builder, useful for chaining This value cannot be null . |