Press on the image to return to the main documentation page.
RichString
Written by Andrew Graham
This library contains RichString and RichStringBuilder objects. These objects are analogous to the normal String and StringBuilder. However RichStrings can contain formatting information that controls how they are drawn when used as the text in a View.
This library also contains a RichStringFormatter object. This object, once suitably initialised, will efficiently format RichStrings passed to it. For long strings that require many changes of format this is much more efficien than using the individual RichString formatting commands.
A RichString supports formatting of its contents at the individual character level. The formatting methods are available in two forms.
The first form accepts a start and index for the region of the RichString to format in the same way that regions of ordinary strings are specified.
The second form uses matched identifiers within the RichString contents to identify the regions to be formatted. The identifiers are arbitary strings used in pairs within the RichString content to bracket the regions to be formatted. Multiple regions within the RichString content can be specified in a single call.
Sets the size of the text in the region relative to the normal text. A value of 1.0 specifies the normal text size. i.e unchanged. A smaller value reduces the size and a larger value increases it.
Sets the size of the text in the regions relative to the normal text. A value of 1.0 specifies the normal text size. i.e unchanged. A smaller value reduces the size and a larger value increases it.
Sets the horizontal size of the text in the region relative to the normal text. A value of 1.0 specifies the normal text size. i.e unchanged. A smaller value reduces the size and a larger value increases it.
Sets the horizontal size of the text in the region relative to the normal text. A value of 1.0 specifies the normal text size. i.e unchanged. A smaller value reduces the size and a larger value increases it.
SetSpanBehaviour (spanbehaviourAsInt)
SpanBehaviour determines how each region behaves when it is edited and sshould be one of the SPAN_XXXX constants. On initialisation the default is SPAN_INCLUSIVE_EXCLUSIVE;
Unlike a RichStringFormatter each region may be added with its own SpanBehaviour.
SPAN_EXCLUSIVE_EXCLUSIVEAsInt
Regions of this type do not expand to include text inserted at either their start or end point. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces nRnBnRn
SPAN_EXCLUSIVE_INCLUSIVEAsInt
Non zero length regions of this type expand to include text inserted at their end point but not at their start point. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces nRrBbRr
SPAN_INCLUSIVE_EXCLUSIVEAsInt
Non zero length regions of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their start point but not at their end point. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces rRbBrRn This was the default in previous versions of RichStringFormatter.
SPAN_INCLUSIVE_INCLUSIVEAsInt
Regions of this type expand to include text inserted at either their start or end point. If two regions adjoin then the rightmost region format appears to be adopted. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces rRbBrRr
RichStringBuilder is a mutable RichString unlike regular RichStrings which are immutable. RichStringBuilder is especially useful when you need to concatenate many RichStrings. Methods of a RichStringBuilder return itself so methods may be repeatedly invoked
Inserts the specified text at the specified offset. Returns the same object, so you can chain methods. Text can be a String, a RichString or a RichStringBuilder.
IsInitializedAsBoolean
LengthAsInt [read only]
Return the number of characters in this RichStringBuilder.
Removes the specified characters. StartOffset - The first character to remove. EndOffset - The ending index. This character will not be removed. Returns the same object, so you can chain methods. Text can be a String, a RichString or a RichStringBuilder.
A RichStringFormatter provides a very efficient way of applying multiple formats to a RichString using matched identifiers within the RichString contents to identify the regions to be formatted.
Each of the RichString format commands iterates along the entire RichString so applying several different formats to the same RichString invokes several iterations. This is fine for short strings like Labels but may take a significant time for longer text with more complicated formatting.
In constrast a RichStringFormatter will format a RichString in a single pass over the string. However in order to accomplish this some initialisation work is necessary to define the formatting tokens that will be accepted by the formatter. Once initialised a RichStringFormatter may be used repeatedly on RichStrings as long as they are using the same tokens for formatting as the RichStringFormatter was initialised to expect.
All formatting tokens must start and end with the characters specified in Initialize. Note that start and end characters need not be different, the same character can be used for both. Formatting tokens are case-sensitive and may be of arbitrary length.
Unlike XML or HTML formatting tokens do not need to be properly nested. They may overlap with no adverse effect and the resulting formatting will be as expected.
Add a token that will be recognised when formatting a RichString. As many tokens as required may be added and once added may be used across multiple invocations of Format.
token - is the token that must start and end with the same characters specified in Initialize. format - is one of the RichStringFormatter FORMAT_XXXX constants data - is any data relevant to the specified format as used in the RichString format methods.
Format (richstringAsandroid.text.SpannableString, spanbehavioursAsInt) AsRichString
Format the specified RichString using the token set built using AddToken. SpanBehaviour determines how each region behaves when it is edited and should be one of the SPAN_XXXX constants.
Returns a new RichString.
FORMAT_BACKCOLORAsInt
FORMAT_COLORAsInt
FORMAT_RELATIVESIZEAsInt
FORMAT_SCALEXAsInt
FORMAT_STRIKETHROUGHAsInt
FORMAT_STYLEAsInt
FORMAT_SUBSCRIPTAsInt
FORMAT_SUPERSCRIPTAsInt
FORMAT_TYPEFACEAsInt
FORMAT_UNDERSCOREAsInt
Initialize (sampletokenAsString)
Initialise the formatter with the start and end characters of each token. The characters are taken from the start and end characters of the sample token. Adding a token that does not start and end correctly will throw an exception.
SPAN_EXCLUSIVE_EXCLUSIVEAsInt
Regions of this type do not expand to include text inserted at either their start or end point. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces nRnBnRn
SPAN_EXCLUSIVE_INCLUSIVEAsInt
Non zero length regions of this type expand to include text inserted at their end point but not at their start point. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces nRrBbRr
SPAN_INCLUSIVE_EXCLUSIVEAsInt
Non zero length regions of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their start point but not at their end point. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces rRbBrRn This was the default in previous versions of RichStringFormatter.
SPAN_INCLUSIVE_INCLUSIVEAsInt
Regions of this type expand to include text inserted at either their start or end point. If two regions adjoin then the rightmost region format appears to be adopted. For example, where R(ed) and B(Blue) are red and blue regions of a string RBR and r(ed) and b(lue) and n(ormal) are the color of inserted characters then this span constant produces rRbBrRr
STYLE_BOLDAsInt
STYLE_BOLD_ITALICAsInt
STYLE_ITALICAsInt
STYLE_NORMALAsInt
UnimplementedTokensAsString()
Returns a String array containing any tokens that were found in the RichString but were not added to the formatter. A zero length array is returned if there are no unimplemented tokens.
UnpairedTokensAsString()
Returns a String array containing any tokens that remained unpaired after Format. A zero length array is returned if there are no unpaired tokens.
Top