public class TextLayout extends Object
TextLayout does not directly generate layouts in the constructor. Instead, it generates layouts only when
you explicitly call doLayout().
You can use setters to change your input data and call doLayout() to lay out the text.
The default values are used for the APIs that are not explicitly set. For details, see the description of each API.| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
TextLayout.CursorMoveDirection |
Enumerates the cursor movement directions.
|
static class |
TextLayout.FontPaddingStrategy |
Enumerates the font padding strategies.
|
static class |
TextLayout.HyphenationStrategy |
Enumerates the hyphenation strategies.
|
static class |
TextLayout.JustifyStrategy |
Enumerates the text justification strategies.
|
static class |
TextLayout.LineBreakStrategy |
Enumerates the line breaking strategies.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
protected Paint |
mPaint |
Used for paint reference.
|
protected RichText |
mRichText |
Used for RichText reference.
|
protected String |
mText |
Used for plain text reference.
|
| 构造器 | 说明 |
|---|---|
TextLayout() |
A constructor used to create a
TextLayout instance without any parameter. |
TextLayout(String text,
Paint paint,
int widthLimit) |
A constructor used to create a
TextLayout instance with the specified text,
paint attribute of the text, and maximum width of the text. |
TextLayout(RichText richText,
Paint paint,
int widthLimit) |
A constructor used to create a
TextLayout instance with the specified richText,
paint attribute of the text, and maximum width of the text. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
doLayout() |
Lays out the text.
|
void |
drawText(Canvas canvas) |
Draws text on the specified
canvas object after the layout is complete. |
int |
getBeginCharIndex(int line) |
Obtains the index of the first character of a specific line in this
TextLayout. |
Point |
getCursorCoordinates(int offset) |
Obtains the cursor position of a specific text offset.
|
float |
getCursorHorizontalCoordinate(int offset) |
Obtains the cursor's horizontal position of a specific text offset.
|
int |
getEndCharIndex(int line) |
Obtains the index of the last character of a specific line, including trailing spaces.
|
int |
getEndCharIndex(int line,
boolean withTrailingSpace) |
Obtains the index of the last character of a specific line in this
TextLayout. |
float |
getHeight() |
Obtains the height of this
TextLayout. |
float |
getLineAscent(int line) |
Obtains the ascent of a specific line in this
TextLayout. |
float |
getLineBaseline(int line) |
Obtains the baseline position of a specific line,
which is a vertical offset from the beginning of this
TextLayout. |
float |
getLineBottom(int line) |
Obtains the bottom position of a specific line.
|
int |
getLineCount() |
Obtains the number of lines in this
TextLayout. |
float |
getLineDescent(int line) |
Obtains the descent of a specific line in this
TextLayout. |
float |
getLineEndPosition(int line) |
Obtains the horizontal coordinate of the end of a specific line, including trailing spaces.
|
float |
getLineEndPosition(int line,
boolean withTrailingSpace) |
Obtains the horizontal coordinate of the end of a specific line.
|
float |
getLineHeight(int line) |
Obtains the height of a specific line.
|
float |
getLineIndentWidth(int line) |
Obtains the indent width of a specific line.
|
int |
getLineIndexByCharIndex(int charIndex) |
Obtains the index of the line where a specific indexed character is located.
|
int |
getLineIndexByCoordinate(float coordinateY) |
Obtains the index of the line that is located at a specific vertical coordinate.
|
float |
getLineLeftPosition(int line) |
Obtains the leftmost horizontal coordinate of the specified line, including indents and trailing spaces.
|
float |
getLineLeftPosition(int line,
boolean withIndentOrSpace) |
Obtains the leftmost horizontal coordinate of the specified line.
|
float |
getLineRightPosition(int line) |
Obtains the rightmost horizontal coordinate of the specified line, including indents and trailing spaces.
|
float |
getLineRightPosition(int line,
boolean withIndentOrSpace) |
Obtains the rightmost horizontal coordinate of the specified line.
|
float |
getLineStartPosition(int line) |
Obtains the horizontal coordinate of the start of a specific line, including indents.
|
float |
getLineStartPosition(int line,
boolean withIndent) |
Obtains the horizontal coordinate of the start of a specific line.
|
float |
getLineTop(int line) |
Obtains the top position of a specific line,
which is a vertical offset from the beginning of this
TextLayout. |
float |
getLineWidth(int line) |
Obtains the width of a specific line, including indents and trailing spaces.
|
float |
getLineWidth(int line,
boolean withIndent,
boolean withTrailingSpace) |
Obtains the width of a specific line.
|
int |
getOffsetByCoordinate(int line,
float coordinateX) |
Obtains the offset of the character that is closest to the given horizontal coordinate in a specific line.
|
int |
getOffsetByCoordinates(float x,
float y) |
Obtains the offset of the character that is closest to the given coordinates.
|
int |
getOffsetByMoveDirection(int offset,
TextLayout.CursorMoveDirection direction) |
Obtains the target cursor offset with the specified direction.
|
float |
getWidth() |
Obtains the width of the longest line including trailing spaces, in this
TextLayout. |
float |
getWidth(boolean withTrailingSpace) |
Obtains the width of the longest line, in this
TextLayout. |
boolean |
isLineWithTab(int line) |
Checks whether the specified line contains tab characters.
|
boolean |
isRtlLine(int line) |
Obtains the main direction of the specified line.
|
void |
setBidiStrategy(BidiAnalyzer.BidiStrategy bidi) |
Sets the bidirectional text recognition strategy for this
TextLayout. |
void |
setFallbackFontMetricsEnabled(Boolean fallbackFontMetricsEnabled) |
Sets whether to enable ascent or descent metrics for fallback fonts.
|
void |
setFontPaddingStrategy(TextLayout.FontPaddingStrategy fontPadding) |
Sets the font padding strategy for this
TextLayout. |
void |
setHyphenationStrategy(TextLayout.HyphenationStrategy hyphenationStrategy) |
Sets the hyphenation strategy for this
TextLayout. |
void |
setJustifyStrategy(TextLayout.JustifyStrategy justifyStrategy) |
Sets the text justification strategy for this
TextLayout. |
void |
setLineBreakStrategy(TextLayout.LineBreakStrategy lineBreakStrategy) |
Sets the line breaking strategy for this
TextLayout. |
void |
setPaint(Paint paint) |
Sets the paint for this
TextLayout. |
void |
setRichText(RichText richText) |
Sets the rich text for this
TextLayout. |
void |
setText(String text) |
Sets the plain text for this
TextLayout. |
void |
setWidthLimit(int widthLimit) |
Sets the width limit for this
TextLayout. |
protected Paint mPaint
null.protected RichText mRichText
null in the plain text case.protected String mText
null in the rich text case.public TextLayout()
TextLayout instance without any parameter.
You need to set parameters before calling doLayout().public TextLayout(String text, Paint paint, int widthLimit) throws NullPointerException
TextLayout instance with the specified text,
paint attribute of the text, and maximum width of the text.
This constructor will leave the other parameters as default.
You can set the other parameters before calling doLayout().text - Indicates the text to set.paint - Indicates the paint used for drawing text.widthLimit - Indicates the maximum width of the text.NullPointerException - Throws this exception if any parameter is null.public TextLayout(RichText richText, Paint paint, int widthLimit) throws NullPointerException
TextLayout instance with the specified richText,
paint attribute of the text, and maximum width of the text.
This constructor will leave the other parameters as default.
You can set the other parameters before calling doLayout().richText - Indicates the RichText.paint - Indicates the paint used for drawing text.widthLimit - Indicates the maximum width of the text.NullPointerException - Throws this exception if any parameter is null.public void setText(String text) throws NullPointerException
TextLayout.text - Indicates the plain text to set.NullPointerException - Throws this exception if text is null.public void setRichText(RichText richText) throws NullPointerException
TextLayout.richText - Indicates the rich text to set.NullPointerException - Throws this exception if richText is null.public void setPaint(Paint paint) throws NullPointerException
TextLayout.paint - Indicates the paint to set.NullPointerException - Throws this exception if paint is null.public void setWidthLimit(int widthLimit)
TextLayout.widthLimit - Indicates the width limit to set.public void setBidiStrategy(BidiAnalyzer.BidiStrategy bidi)
TextLayout.
The default bidirectional text recognition strategy is FIRSTSTRONG_OR_LTR.
bidi - Indicates the bidirectional text recognition strategy to set.public void setFallbackFontMetricsEnabled(Boolean fallbackFontMetricsEnabled)
fallbackFontMetricsEnabled - Specifies whether to enable fallback font metrics.
The value true means to enable ascent or descent metrics for fallback fonts,
and false means the opposite. The default value is true.public void setFontPaddingStrategy(TextLayout.FontPaddingStrategy fontPadding)
TextLayout.fontPadding - Indicates the font padding strategy to set.public void setLineBreakStrategy(TextLayout.LineBreakStrategy lineBreakStrategy)
TextLayout.lineBreakStrategy - Indicates the line breaking strategy to set.public void setHyphenationStrategy(TextLayout.HyphenationStrategy hyphenationStrategy)
TextLayout.hyphenationStrategy - Indicates the hyphenation strategy to set.public void setJustifyStrategy(TextLayout.JustifyStrategy justifyStrategy)
TextLayout.justifyStrategy - Indicates the text justification strategy to set.public void doLayout()
The speed of this method depends on the previous setters.
The layout can be obtained through a Get method only after doLayout() is called.
public void drawText(Canvas canvas)
canvas object after the layout is complete.
You may need to perform translation and clipping manually.
canvas - Indicates the Canvas object.Canvas.clipRect(Rect),
Canvas.translate(float, float)public float getWidth()
TextLayout.public float getWidth(boolean withTrailingSpace)
TextLayout.withTrailingSpace - Specifies whether to include trailing spaces.
The value true means to include the trailing spaces, and false means the opposite.public float getHeight()
TextLayout. The height is restricted by the paint used to draw the text.public int getLineCount()
TextLayout. The number of lines is restricted by the paint used
to draw the text.public float getLineWidth(int line)
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineWidth(int line,
boolean withIndent,
boolean withTrailingSpace)
line - Indicates the line.withIndent - Specifies whether to include indents. The value true means to include indents,
and false means the opposite.withTrailingSpace - Specifies whether to include trailing spaces. The value true means to include
trailing spaces, and false means the opposite.-1.0f if the value of line is invalid.public float getLineIndentWidth(int line)
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineHeight(int line)
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineBaseline(int line)
TextLayout.line - Indicates the line.-1.0f if the value of line is invalid.public float getLineTop(int line)
TextLayout.line - Indicates the line.-1.0f if the value of line is invalid.public float getLineBottom(int line)
TextLayout.line - Indicates the line.-1.0f if the value of line is invalid.public float getLineAscent(int line)
TextLayout.
Note that the ascent is a relative and negative value from the baseline.line - Indicates the line.1.0f if the value of line is invalid.public float getLineDescent(int line)
TextLayout.
Note that the descent is a relative and positive value from the baseline.line - Indicates the line.-1.0f if the value of line is invalid.public int getBeginCharIndex(int line)
TextLayout.line - Indicates the line.-1 if the value of line is invalid.public int getEndCharIndex(int line)
line - Indicates the line.-1 if the value of line is invalid.public int getEndCharIndex(int line,
boolean withTrailingSpace)
TextLayout.line - Indicates the line.withTrailingSpace - Specifies whether to include trailing spaces. The value true means to include
trailing spaces, and false means the opposite.-1 if the value of line is invalid.public int getLineIndexByCharIndex(int charIndex)
charIndex - Indicates the index of the character.-1 if the value of charIndex is invalid.public int getLineIndexByCoordinate(float coordinateY)
coordinateY - Indicates the vertical coordinate.getLineCount() to obtain the total line number.public float getLineStartPosition(int line)
Note that the start position is on the right side in the RTL case.
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineStartPosition(int line,
boolean withIndent)
Note that the start position is on the right side in the RTL case.
line - Indicates the line.withIndent - Specifies whether to include indents. The value true means to include indents,
and false means the opposite.-1.0f if the value of line is invalid.public float getLineLeftPosition(int line)
This method is the same as getLineStartPosition(int) in the LTR case.
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineLeftPosition(int line,
boolean withIndentOrSpace)
This method is the same as getLineStartPosition(int,boolean) in the LTR case.
line - Indicates the line.withIndentOrSpace - Specifies whether to include indents and trailing spaces.
The value true means to include indents and trailing spaces, and false
means the opposite.-1.0f if the value of line is invalid.public float getLineEndPosition(int line)
Note that the end position is on the left side in the RTL case.
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineEndPosition(int line,
boolean withTrailingSpace)
Note that the end position is on the left side in the RTL case.
line - Indicates the line.withTrailingSpace - Specifies whether to include trailing spaces.
The value true means to include trailing spaces, and false means the opposite.-1.0f if the value of line is invalid.public float getLineRightPosition(int line)
This method is the same as getLineEndPosition(int) in the LTR case.
line - Indicates the line.-1.0f if the value of line is invalid.public float getLineRightPosition(int line,
boolean withIndentOrSpace)
This method is the same as getLineEndPosition(int,boolean) in the LTR case.
line - Indicates the line.withIndentOrSpace - Specifies whether to include indents and trailing spaces.
The value true means to include indents and trailing spaces, and false means the opposite.-1.0f if the value of line is invalid.public boolean isRtlLine(int line)
line - Indicates the line.false if the value of line is invalid; returns true otherwise.public boolean isLineWithTab(int line)
line - Indicates the line.true if the specified line contains any tab character; returns false otherwise.public int getOffsetByCoordinates(float x,
float y)
Note that the offset ranges from 0 to the text length,
that is, the offset may be the last character index plus 1.
x - Indicates the x-coordinate.y - Indicates the y-coordinate.public int getOffsetByCoordinate(int line,
float coordinateX)
Note that the offset ranges from 0 to the text length,
that is, the offset may be the last character index plus 1.
line - Indicates the line.coordinateX - Indicates the given horizontal position.-1 if the value of line is invalid.public Point getCursorCoordinates(int offset)
offset - Indicates the text offset.public float getCursorHorizontalCoordinate(int offset)
offset - Indicates the text offset.public int getOffsetByMoveDirection(int offset,
TextLayout.CursorMoveDirection direction)
Note that this method does not move the cursor, but only returns the offset of the cursor.
offset - Indicates the current cursor offset.direction - Indicates the direction of cursor movement.