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);
}
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 & blog\" target=\"_blank\" notes-data-click=\"best_period_ever_feat\">taliwut & 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"));
}
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));
}
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());
}
}
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);
}
Aggregations