Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class RichText
- Object
- RichText
Immutable styled text: a sequence of runs, each a string with one TextStyle, and inline
objects – fields, note references, pictures and embedded components – each of which takes
exactly one character, OBJECT.
Adjacent text runs with equal styles are merged and empty runs are dropped, so two texts that
read the same and look the same are equals. An object run is never merged.
Nested types
class RichText.Visitor | Receives the runs of a text in order. |
class RichText.Builder | Accumulates runs into a RichText. |
interface RichText.StyleMapper | Computes a new style from an old one, for restyle. |
Fields
public static final char OBJECT = '' | The character an inline object occupies, U+FFFC OBJECT REPLACEMENT CHARACTER. |
public static final RichText EMPTY | The empty text. |
Methods
Inherited methods
Field details
OBJECT
public static final char OBJECT = ''The character an inline object occupies, U+FFFC OBJECT REPLACEMENT CHARACTER.
EMPTY
public static final RichText EMPTYThe empty text.
Method details
of
public static RichText of(String s)Plain text in the default style.
Parameters
sString- the characters
Returns
the text
of
public static RichText of(String s, TextStyle style)Text in a single style.
Parameters
sString- the characters
styleTextStyle- their style
Returns
the text
of
public static RichText of(Inline object, TextStyle style)A text holding one inline object.
Parameters
objectInline- the object
styleTextStyle- the style of its character
Returns
the text
length
public int length()The number of characters.
Returns
the length
isEmpty
public boolean isEmpty()Whether the text has no characters.
Returns
true when empty
runCount
public int runCount()The number of runs, inline objects included.
Returns
the run count
runText
public String runText(int index)The characters of one run;
OBJECT for an inline object.Parameters
indexint- the run, from zero
Returns
the run’s text
runStyle
public TextStyle runStyle(int index)The style of one run.
Parameters
indexint- the run, from zero
Returns
the run’s style
runObject
public Inline runObject(int index)The inline object of one run.
Parameters
indexint- the run, from zero
Returns
the object, or null for a text run
objectAt
public Inline objectAt(int offset)The inline object at a character offset.
Parameters
offsetint- the character offset
Returns
the object, or null when the character is not an object
hasObjects
public boolean hasObjects()Whether the text holds any inline object.
Returns
true when it does
plainText
public String plainText()The characters without formatting. Every inline object is one
OBJECT character.Returns
the plain text
displayText
public String displayText()The text as a reader sees it: a field shows its result, a note reference its mark, and
other objects nothing.
Returns
the readable text
styleAt
public TextStyle styleAt(int offset)The style at a character offset. At the end of the text, and for an empty text, this is
the style of the last run, so text typed there continues it.
Parameters
offsetint- the character offset
Returns
the style in effect
visit
public void visit(RichText.Visitor visitor)Calls the visitor once per run, in order.
Parameters
visitorRichText.Visitor- the receiver
substring
public RichText substring(int start, int end)The characters in
[start, end) with their styles and objects.Parameters
startint- the first offset, inclusive
endint- the last offset, exclusive
Returns
the slice
insert
public RichText insert(int offset, RichText insert)This text with
insert placed at an offset.Parameters
offsetint- where to insert
insertRichText- the text to insert
Returns
the new text
delete
public RichText delete(int start, int end)This text without the characters in
[start, end).Parameters
startint- the first offset, inclusive
endint- the last offset, exclusive
Returns
the new text
append
public RichText append(RichText other)This text followed by another.
Parameters
otherRichText- the text to append
Returns
the new text
restyle
public RichText restyle(int start, int end, RichText.StyleMapper mapper)Restyles the characters in
[start, end).Parameters
startint- the first offset, inclusive
endint- the last offset, exclusive
mapperRichText.StyleMapper- computes each run’s new style from its old one
Returns
the new text
withObject
public RichText withObject(int offset, Inline object)This text with the object at
offset replaced.Parameters
offsetint- the object’s character
objectInline- the replacement
Returns
the new text
styles
public List<TextStyle> styles()The distinct styles used, in order of first use.
Returns
an unmodifiable list
equals
public boolean equals(Object o)Two texts are equal when they have the same runs and objects.
Parameters
oObject- the other object
Returns
true when equal
hashCode
public int hashCode()A hash consistent with
equals.Returns
the hash
toString
public String toString()The plain text, for debugging.
Returns
the characters