Class StyleSpan

All Implemented Interfaces:
ParcelableSpan, UpdateAppearance, UpdateLayout, Parcelable

public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan
Span that allows setting the style of the text it's attached to. Possible styles are: Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC and Typeface.BOLD_ITALIC.

Note that styles are cumulative -- if both bold and italic are set in separate spans, or if the base style is bold and a span calls for italic, you get bold italic.
You can't turn off a style from the base style.

For example, the StyleSpan can be used like this:

 SpannableString string = new SpannableString("Bold and italic text");
 string.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 string.setSpan(new StyleSpan(Typeface.ITALIC), 9, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
  • Constructor Details

    • StyleSpan

      public StyleSpan(int style)
      Creates a StyleSpan from a style.
      Parameters:
      style - An integer constant describing the style for this span. Examples include bold, italic, and normal. Values are constants defined in TextPaint.
    • StyleSpan

      public StyleSpan(@NonNull Parcel src)
      Creates a StyleSpan from a parcel.
  • Method Details

    • getSpanTypeId

      public int getSpanTypeId()
      Description copied from interface: ParcelableSpan
      Return a special type identifier for this span class.
      Specified by:
      getSpanTypeId in interface ParcelableSpan
    • writeToParcel

      public void writeToParcel(@NonNull Parcel dest, int flags)
      Description copied from interface: Parcelable
      The subclass implements the method to flatten its contents by calling the methods of Parcel for its primitive values.
      Specified by:
      writeToParcel in interface Parcelable
      Parameters:
      dest - the parcel to write the object's data to
      flags - the flags about how the object should be written
    • getStyle

      public int getStyle()
      Returns the style constant defined in TextPaint.
    • updateMeasureState

      public void updateMeasureState(@NonNull TextPaint paint)
      Description copied from class: MetricAffectingSpan
      Classes that extend MetricAffectingSpan implement this method to update the text formatting in a way that can change the width or height of characters.
      Specified by:
      updateMeasureState in class MetricAffectingSpan
      Parameters:
      paint - the paint used for measuring the text