Search in sources :

Example 76 with Spanned

use of android.text.Spanned in project WordPress-Android by wordpress-mobile.

the class NotesParseTest method testParagraphInListItem1.

public void testParagraphInListItem1() {
    String text = "<li><p>Paragraph in li</p></li>";
    Spanned spanned = HtmlUtils.fromHtml(text);
    // if this didn't throw a RuntimeException we're ok
    assertNotNull(spanned);
}
Also used : Spanned(android.text.Spanned)

Example 77 with Spanned

use of android.text.Spanned in project WordPress-Android by wordpress-mobile.

the class NotesParseTest method testSpanInListItemFullTest.

public void testSpanInListItemFullTest() {
    String text = "<p>Au Mercredi 18 septembre 2013 vous avez pulvérisé votre précédent record de follows enregistrés en un seul jour, sur votre blog <a href=\"http://taliwutblog.wordpress.com\" title=\"taliwut &amp; blog\" target=\"_blank\" notes-data-click=\"best_period_ever_feat\">taliwut &amp; blog</a>. Super!</p><ul><li><span  class=\"wpn-feat-current-record-title\">Current Record: </span><span class=\"wpn-feat-new-record-count\">20</span></li><li><span  class=\"wpn-feat-old-record-title\">Old Record: </span><span class=\"wpn-feat-old-record-count\">1</span></li></ul>";
    Spanned spanned = HtmlUtils.fromHtml(text);
    assertTrue(spanned.toString().contains("Current Record: 20\nOld Record: 1\n"));
}
Also used : Spanned(android.text.Spanned)

Example 78 with Spanned

use of android.text.Spanned in project WordPress-Android by wordpress-mobile.

the class CommentUtils method displayHtmlComment.

/*
     * displays comment text as html, including retrieving images
     */
public static void displayHtmlComment(TextView textView, String content, int maxImageSize, ImageLoader imageLoader) {
    if (textView == null) {
        return;
    }
    if (content == null) {
        textView.setText(null);
        return;
    }
    // skip performance hit of html conversion if content doesn't contain html
    if (!content.contains("<") && !content.contains("&")) {
        content = content.trim();
        textView.setText(content);
        // make sure unnamed links are clickable
        if (content.contains("://")) {
            Linkify.addLinks(textView, Linkify.WEB_URLS);
        }
        return;
    }
    // convert emoticons first (otherwise they'll be downloaded)
    content = EmoticonsUtils.replaceEmoticonsWithEmoji(content);
    // now convert to HTML with an image getter that enforces a max image size
    final Spanned html;
    if (maxImageSize > 0 && content.contains("<img")) {
        Drawable loading = ContextCompat.getDrawable(textView.getContext(), R.drawable.legacy_dashicon_format_image_big_grey);
        Drawable failed = ContextCompat.getDrawable(textView.getContext(), R.drawable.ic_notice_grey_500_48dp);
        html = HtmlUtils.fromHtml(content, new WPImageGetter(textView, maxImageSize, imageLoader, loading, failed));
    } else {
        html = HtmlUtils.fromHtml(content);
    }
    // remove extra \n\n added by Html.convert()
    int start = 0;
    int end = html.length();
    while (start < end && Character.isWhitespace(html.charAt(start))) {
        start++;
    }
    while (end > start && Character.isWhitespace(html.charAt(end - 1))) {
        end--;
    }
    textView.setText(html.subSequence(start, end));
}
Also used : Drawable(android.graphics.drawable.Drawable) WPImageGetter(org.wordpress.android.util.helpers.WPImageGetter) Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 79 with Spanned

use of android.text.Spanned in project android_frameworks_base by crdroidandroid.

the class SuggestionsPopupWindowTest method testTextAppearanceInSuggestionsPopup.

@SmallTest
public void testTextAppearanceInSuggestionsPopup() {
    final String text = "abc def ghi";
    final String[] singleWordCandidates = { "DEF", "Def" };
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final String[] multiWordCandidates = { "ABC DEF GHI", "Abc Def Ghi" };
    final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(), multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final TypedArray array = getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
    final int id = array.getResourceId(com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
    array.recycle();
    final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
    final TextPaint tmpTp = new TextPaint();
    expectedSpan.updateDrawState(tmpTp);
    final int expectedHighlightTextColor = tmpTp.getColor();
    final float expectedHighlightTextSize = tmpTp.getTextSize();
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    // *XX* means that XX is highlighted.
    for (int i = 0; i < 2; i++) {
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(replaceText(text));
        setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
        setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
        showSuggestionsPopup();
        assertSuggestionsPopupIsDisplayed();
        assertSuggestionsPopupContainsItem("abc DEF ghi");
        assertSuggestionsPopupContainsItem("abc Def ghi");
        assertSuggestionsPopupContainsItem("ABC DEF GHI");
        assertSuggestionsPopupContainsItem("Abc Def Ghi");
        assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
        onSuggestionsPopup().check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException e) {
                final ListView listView = (ListView) view.findViewById(com.android.internal.R.id.suggestionContainer);
                assertNotNull(listView);
                final int childNum = listView.getChildCount();
                assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum);
                for (int j = 0; j < childNum; j++) {
                    final TextView suggestion = (TextView) listView.getChildAt(j);
                    assertNotNull(suggestion);
                    final Spanned spanned = (Spanned) suggestion.getText();
                    assertNotNull(spanned);
                    // Check that the suggestion item order is kept.
                    final String expectedText;
                    if (j < singleWordCandidates.length) {
                        expectedText = "abc " + singleWordCandidates[j] + " ghi";
                    } else {
                        expectedText = multiWordCandidates[j - singleWordCandidates.length];
                    }
                    assertEquals(expectedText, spanned.toString());
                    // Check that the text is highlighted with correct color and text size.
                    final TextAppearanceSpan[] taSpan = spanned.getSpans(text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
                    assertEquals(1, taSpan.length);
                    TextPaint tp = new TextPaint();
                    taSpan[0].updateDrawState(tp);
                    assertEquals(expectedHighlightTextColor, tp.getColor());
                    assertEquals(expectedHighlightTextSize, tp.getTextSize());
                    // Check the correct part of the text is highlighted.
                    final int expectedStart;
                    final int expectedEnd;
                    if (j < singleWordCandidates.length) {
                        expectedStart = text.indexOf('d');
                        expectedEnd = text.indexOf('f') + 1;
                    } else {
                        expectedStart = 0;
                        expectedEnd = text.length();
                    }
                    assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
                    assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
                }
            }
        });
        pressBack();
        onView(withId(R.id.textview)).inRoot(withDecorView(is(getActivity().getWindow().getDecorView()))).perform(clearText());
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) RootMatchers.withDecorView(android.support.test.espresso.matcher.RootMatchers.withDecorView) DragHandleUtils.onHandleView(android.widget.espresso.DragHandleUtils.onHandleView) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) ViewAssertion(android.support.test.espresso.ViewAssertion) TypedArray(android.content.res.TypedArray) SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 80 with Spanned

use of android.text.Spanned in project android_frameworks_base by crdroidandroid.

the class IconMarginSpan method drawLeadingMargin.

public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
    int st = ((Spanned) text).getSpanStart(this);
    int itop = layout.getLineTop(layout.getLineForOffset(st));
    if (dir < 0)
        x -= mBitmap.getWidth();
    c.drawBitmap(mBitmap, x, itop, p);
}
Also used : Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Aggregations

Spanned (android.text.Spanned)204 Paint (android.graphics.Paint)81 TextPaint (android.text.TextPaint)63 Spannable (android.text.Spannable)32 SpannableStringBuilder (android.text.SpannableStringBuilder)27 SpannableString (android.text.SpannableString)25 SuggestionSpan (android.text.style.SuggestionSpan)24 Editable (android.text.Editable)22 TextAppearanceSpan (android.text.style.TextAppearanceSpan)15 SpannedString (android.text.SpannedString)14 TypedArray (android.content.res.TypedArray)12 URLSpan (android.text.style.URLSpan)12 View (android.view.View)12 Context (android.content.Context)10 StyleSpan (android.text.style.StyleSpan)9 InputMethodManager (android.view.inputmethod.InputMethodManager)9 TextView (android.widget.TextView)9 Parcelable (android.os.Parcelable)8 WordIterator (android.text.method.WordIterator)7 CharacterStyle (android.text.style.CharacterStyle)7