Class SpannableStringBuilder

java.lang.Object
icyllis.modernui.text.SpannableStringBuilder
All Implemented Interfaces:
Editable, GetChars, Spannable, Spanned, Appendable, CharSequence

public class SpannableStringBuilder extends Object implements Editable, Spannable, GetChars, Appendable
This is the class for text whose content and markup can both be changed.
  • Field Details

    • MARKER

      public static final org.apache.logging.log4j.Marker MARKER
  • Constructor Details

    • SpannableStringBuilder

      public SpannableStringBuilder()
      Create a new SpannableStringBuilder with empty contents
    • SpannableStringBuilder

      public SpannableStringBuilder(@Nonnull CharSequence text)
      Create a new SpannableStringBuilder containing a copy of the specified text, including its spans if any.
    • SpannableStringBuilder

      public SpannableStringBuilder(@Nonnull CharSequence text, int start, int end)
      Create a new SpannableStringBuilder containing a copy of the specified slice of the specified text, including its spans if any.
  • Method Details

    • valueOf

      @Nonnull public static SpannableStringBuilder valueOf(@Nonnull CharSequence source)
    • charAt

      public char charAt(int where)
      Return the char at the specified offset within the buffer.
      Specified by:
      charAt in interface CharSequence
    • length

      public int length()
      Return the number of chars in the buffer.
      Specified by:
      length in interface CharSequence
    • insert

      public SpannableStringBuilder insert(int where, @Nonnull CharSequence tb, int start, int end)
      Convenience for replace(where, where, text, start, end)
      Specified by:
      insert in interface Editable
      See Also:
    • insert

      public SpannableStringBuilder insert(int where, @Nonnull CharSequence tb)
      Convenience for replace(where, where, text, 0, text.length());
      Specified by:
      insert in interface Editable
      See Also:
    • delete

      public SpannableStringBuilder delete(int start, int end)
      Convenience for replace(st, en, "", 0, 0)
      Specified by:
      delete in interface Editable
      See Also:
    • clear

      public void clear()
      Convenience for replace(0, length(), "", 0, 0). Note that this clears the text, not the spans; use clearSpans() if you need that.
      Specified by:
      clear in interface Editable
      See Also:
    • clearSpans

      public void clearSpans()
      Removes all spans from the Editable, as if by calling removeSpan(int, int) on each of them.
      Specified by:
      clearSpans in interface Editable
    • append

      public SpannableStringBuilder append(@Nonnull CharSequence text)
      Convenience for replace(length(), length(), text, 0, text.length())
      Specified by:
      append in interface Appendable
      Specified by:
      append in interface Editable
      See Also:
    • append

      public SpannableStringBuilder append(@Nonnull CharSequence text, @Nonnull Object what, int flags)
      Appends the character sequence text and spans what over the appended part. See Spanned for an explanation of what the flags mean.
      Parameters:
      text - the character sequence to append.
      what - the object to be spanned over the appended text.
      flags - see Spanned.
      Returns:
      this SpannableStringBuilder.
    • append

      public SpannableStringBuilder append(@Nonnull CharSequence text, int start, int end)
      Convenience for replace(length(), length(), text, start, end)
      Specified by:
      append in interface Appendable
      Specified by:
      append in interface Editable
      See Also:
    • append

      public SpannableStringBuilder append(char text)
      Convenience for append(String.valueOf(text)).
      Specified by:
      append in interface Appendable
      Specified by:
      append in interface Editable
      See Also:
    • replace

      public SpannableStringBuilder replace(int start, int end, @Nonnull CharSequence tb)
      Convenience for replace(st, en, text, 0, text.length())
      Specified by:
      replace in interface Editable
      See Also:
    • replace

      public SpannableStringBuilder replace(int start, int end, @Nonnull CharSequence tb, int tbstart, int tbend)
      Replaces the specified range (start…end) of text in this Editable with a copy of the slice tbstart…tbend from tb. The destination slice may be empty, in which case the operation is an insertion, or the source slice may be empty, in which case the operation is a deletion.

      Before the change is committed, each filter that was set with setFilters(icyllis.modernui.text.InputFilter[]) is given the opportunity to modify the source text.

      If source is Spanned, the spans from it are preserved into the Editable. Existing spans within the Editable that entirely cover the replaced range are retained, but any that were strictly within the range that was replaced are removed. If the source contains a span with Spanned.SPAN_PARAGRAPH flag, and it does not satisfy the paragraph boundary constraint, it is not retained. As a special case, the cursor position is preserved even when the entire range where it is located is replaced.

      Specified by:
      replace in interface Editable
      Returns:
      a reference to this object.
      See Also:
    • setSpan

      public void setSpan(@Nonnull Object what, int start, int end, int flags)
      Mark the specified range of text with the specified object. The flags determine how the span will behave when text is inserted at the start or end of the span's range.
      Specified by:
      setSpan in interface Spannable
      Parameters:
      what - the markup object
      start - the start char index of the span
      end - the end char index of the span
      flags - the flags of the span
    • removeSpan

      public void removeSpan(@Nonnull Object what)
      Remove the specified markup object from the buffer.
      Specified by:
      removeSpan in interface Spannable
      Parameters:
      what - markup object to remove
    • removeSpan

      public void removeSpan(@Nonnull Object what, int flags)
      Remove the specified markup object from the buffer.
      Specified by:
      removeSpan in interface Spannable
      Parameters:
      what - markup object to remove
      flags - flags
    • getSpanStart

      public int getSpanStart(@Nonnull Object what)
      Return the buffer offset of the beginning of the specified markup object, or -1 if it is not attached to this buffer.
      Specified by:
      getSpanStart in interface Spanned
      Parameters:
      what - markup object
      Returns:
      the start char index
    • getSpanEnd

      public int getSpanEnd(@Nonnull Object what)
      Return the buffer offset of the end of the specified markup object, or -1 if it is not attached to this buffer.
      Specified by:
      getSpanEnd in interface Spanned
      Parameters:
      what - markup object
      Returns:
      the end char index
    • getSpanFlags

      public int getSpanFlags(@Nonnull Object what)
      Return the flags of the end of the specified markup object, or 0 if it is not attached to this buffer.
      Specified by:
      getSpanFlags in interface Spanned
      Parameters:
      what - markup object
      Returns:
      the flags
    • getSpans

      @Nonnull public <T> List<T> getSpans(int queryStart, int queryEnd, @Nullable Class<? extends T> kind, @Nullable List<T> out)
      Return an array of the spans of the specified type that overlap the specified range of the buffer. The kind may be Object.class to get a list of all the spans regardless of type.
      Specified by:
      getSpans in interface Spanned
      Parameters:
      queryStart - start char index of the slice
      queryEnd - end char index of the slice
      kind - markup class
      out - the list that receives method results
      Returns:
      the list of results
    • getSpans

      @Nonnull public <T> List<T> getSpans(int queryStart, int queryEnd, @Nullable Class<? extends T> kind, boolean sortByInsertionOrder, @Nullable List<T> dest)
      Return an array of the spans of the specified type that overlap the specified range of the buffer. The kind may be Object.class to get a list of all the spans regardless of type.
      Type Parameters:
      T - Markup type.
      Parameters:
      queryStart - Start index.
      queryEnd - End index.
      kind - Class type to search for.
      sortByInsertionOrder - If true the results are sorted by the insertion order.
      Returns:
      List of the spans.
    • nextSpanTransition

      public int nextSpanTransition(int start, int limit, @Nullable Class<?> kind)
      Return the next offset after start but less than or equal to limit where a span of the specified type begins or ends.
      Specified by:
      nextSpanTransition in interface Spanned
      Parameters:
      start - start char index of the slice
      limit - end char index of the slice
      kind - the markup type
      Returns:
      transition point
    • subSequence

      public CharSequence subSequence(int start, int end)
      Return a new CharSequence containing a copy of the specified range of this buffer, including the overlapping spans.
      Specified by:
      subSequence in interface CharSequence
    • getChars

      public void getChars(int start, int end, char[] dest, int destoff)
      Copy the specified range of chars from this buffer into the specified array, beginning at the specified offset.
      Specified by:
      getChars in interface GetChars
    • toString

      public String toString()
      Return a String containing a copy of the chars in this buffer.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • substring

      public String substring(int start, int end)
      Return a String containing a copy of the chars in this buffer, limited to the [start, end) range.
    • getTextWatcherDepth

      public int getTextWatcherDepth()
      Returns the depth of TextWatcher callbacks. Returns 0 when the object is not handling TextWatchers. A return value greater than 1 implies that a TextWatcher caused a change that recursively triggered a TextWatcher.
    • setFilters

      public void setFilters(@Nonnull InputFilter[] filters)
      Sets the series of filters that will be called in succession whenever the text of this Editable is changed, each of which has the opportunity to limit or transform the text that is being inserted.
      Specified by:
      setFilters in interface Editable
    • getFilters

      @Nonnull public InputFilter[] getFilters()
      Returns the array of input filters that are currently applied to changes to this Editable.
      Specified by:
      getFilters in interface Editable
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object