Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class RichText

  1. Object
  2. 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.VisitorReceives the runs of a text in order.
class RichText.BuilderAccumulates runs into a RichText.
interface RichText.StyleMapperComputes 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 EMPTYThe empty text.

Methods

public static RichText of(String s)Plain text in the default style.
public static RichText of(String s, TextStyle style)Text in a single style.
public static RichText of(Inline object, TextStyle style)A text holding one inline object.
public int length()The number of characters.
public boolean isEmpty()Whether the text has no characters.
public int runCount()The number of runs, inline objects included.
public String runText(int index)The characters of one run; OBJECT for an inline object.
public TextStyle runStyle(int index)The style of one run.
public Inline runObject(int index)The inline object of one run.
public Inline objectAt(int offset)The inline object at a character offset.
public boolean hasObjects()Whether the text holds any inline object.
public String plainText()The characters without formatting.
public String displayText()The text as a reader sees it: a field shows its result, a note reference its mark, and other objects nothing.
public TextStyle styleAt(int offset)The style at a character offset.
public void visit(RichText.Visitor visitor)Calls the visitor once per run, in order.
public RichText substring(int start, int end)The characters in [start, end) with their styles and objects.
public RichText insert(int offset, RichText insert)This text with insert placed at an offset.
public RichText delete(int start, int end)This text without the characters in [start, end).
public RichText append(RichText other)This text followed by another.
public RichText restyle(int start, int end, RichText.StyleMapper mapper)Restyles the characters in [start, end).
public RichText withObject(int offset, Inline object)This text with the object at offset replaced.
public List<TextStyle> styles()The distinct styles used, in order of first use.
public boolean equals(Object o)Two texts are equal when they have the same runs and objects.
public int hashCode()A hash consistent with equals.
public String toString()The plain text, for debugging.

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 EMPTY
The empty text.

Method details

of

public static RichText of(String s)
Plain text in the default style.

Parameters

s String
the characters

Returns

the text

of

public static RichText of(String s, TextStyle style)
Text in a single style.

Parameters

s String
the characters
style TextStyle
their style

Returns

the text

of

public static RichText of(Inline object, TextStyle style)
A text holding one inline object.

Parameters

object Inline
the object
style TextStyle
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

index int
the run, from zero

Returns

the run’s text

runStyle

public TextStyle runStyle(int index)
The style of one run.

Parameters

index int
the run, from zero

Returns

the run’s style

runObject

public Inline runObject(int index)
The inline object of one run.

Parameters

index int
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

offset int
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

offset int
the character offset

Returns

the style in effect

visit

public void visit(RichText.Visitor visitor)
Calls the visitor once per run, in order.

Parameters

visitor RichText.Visitor
the receiver

substring

public RichText substring(int start, int end)
The characters in [start, end) with their styles and objects.

Parameters

start int
the first offset, inclusive
end int
the last offset, exclusive

Returns

the slice

insert

public RichText insert(int offset, RichText insert)
This text with insert placed at an offset.

Parameters

offset int
where to insert
insert RichText
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

start int
the first offset, inclusive
end int
the last offset, exclusive

Returns

the new text

append

public RichText append(RichText other)
This text followed by another.

Parameters

other RichText
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

start int
the first offset, inclusive
end int
the last offset, exclusive
mapper RichText.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

offset int
the object’s character
object Inline
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

o Object
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