Java Code Examples for android.text.SpannedString#getSpans()

The following examples show how to use android.text.SpannedString#getSpans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MarkdownBuilderTest.java    From user-interface-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void textWithQuote() {
    SpannedString result = builder.markdownToSpans("Text\n> Quote");

    assertEquals("Text\nQuote", result.toString());
    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(3, spans.length);

    StyleSpan styleSpan = (StyleSpan) spans[0];
    assertEquals(Typeface.ITALIC, styleSpan.getStyle());
    assertEquals(5, result.getSpanStart(styleSpan));
    assertEquals(10, result.getSpanEnd(styleSpan));
    LeadingMarginSpan leadingMarginSpan = (LeadingMarginSpan) spans[1];
    assertEquals(5, result.getSpanStart(leadingMarginSpan));
    assertEquals(10, result.getSpanEnd(leadingMarginSpan));
    RelativeSizeSpan relativeSizeSpan = (RelativeSizeSpan) spans[2];
    assertEquals(5, result.getSpanStart(relativeSizeSpan));
    assertEquals(10, result.getSpanEnd(relativeSizeSpan));
}
 
Example 2
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 6 votes vote down vote up
public static Matcher<View> withColors(final int... colors) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public boolean matchesSafely(TextView textView) {
      SpannedString text = (SpannedString) textView.getText();
      ForegroundColorSpan[] spans = text.getSpans(0, text.length(), ForegroundColorSpan.class);
      if (spans.length != colors.length) {
        return false;
      }
      for (int i = 0; i < colors.length; ++i) {
        if (spans[i].getForegroundColor() != colors[i]) {
          return false;
        }
      }
      return true;
    }
    @Override public void describeTo(Description description) {
      description.appendText("has colors:");
      for (int color : colors) {
        description.appendText(" " + getHexColor(color));
      }
    }
  };
}
 
Example 3
Source File: MarkdownBuilderTest.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void text() {
    SpannedString result = builder.markdownToSpans("Text");

    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(0, spans.length);
}
 
Example 4
Source File: MarkdownBuilderTest.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void textWithBulletPoints() {
    SpannedString result = builder.markdownToSpans("Points\n* one\n+ two");

    assertEquals("Points\none\ntwo", result.toString());
    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(2, spans.length);

    BulletPointSpan bulletSpan = (BulletPointSpan) spans[0];
    assertEquals(7, result.getSpanStart(bulletSpan));
    assertEquals(11, result.getSpanEnd(bulletSpan));
    BulletPointSpan bulletSpan2 = (BulletPointSpan) spans[1];
    assertEquals(11, result.getSpanStart(bulletSpan2));
    assertEquals(14, result.getSpanEnd(bulletSpan2));
}
 
Example 5
Source File: MarkdownBuilderTest.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void textWithCode() {
    SpannedString result = builder.markdownToSpans("Text `code`");

    assertEquals("Text code", result.toString());
    Object[] spans = result.getSpans(0, result.length(), Object.class);
    assertEquals(1, spans.length);

    CodeBlockSpan codeSpan = (CodeBlockSpan) spans[0];
    assertEquals(5, result.getSpanStart(codeSpan));
    assertEquals(9, result.getSpanEnd(codeSpan));
}
 
Example 6
Source File: TextViewUtils.java    From react-native-navigation with MIT License 4 votes vote down vote up
@ColorInt
public static int getTextColor(TextView view) {
    SpannedString text = new SpannedString(view.getText());
    ForegroundColorSpan[] spans = text.getSpans(0, text.length(), ForegroundColorSpan.class);
    return spans.length == 0 ? view.getCurrentTextColor() : spans[0].getForegroundColor();
}
 
Example 7
Source File: TextViewUtils.java    From react-native-navigation with MIT License 4 votes vote down vote up
public static float getTextSize(TextView view) {
    SpannedString text = new SpannedString(view.getText());
    AbsoluteSizeSpan[] spans = text.getSpans(0, text.length(), AbsoluteSizeSpan.class);
    return spans.length == 0 ? -1 : spans[0].getSize();
}