Search in sources :

Example 26 with URLSpan

use of android.text.style.URLSpan in project Android-Rich-text-Editor by chinalwb.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof ARE_Span) {
                out.append(((ARE_Span) style[j]).getHtml());
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    // Application application = ActivityThread.currentApplication();
                    // float density = application.getResources().getDisplayMetrics().density;
                    float density = 1.5f;
                    sizeDip /= density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) ARE_Span(com.chinalwb.are.spans.ARE_Span) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 27 with URLSpan

use of android.text.style.URLSpan in project Android-Rich-text-Editor by chinalwb.

the class ARE_Link method insertLink.

private void insertLink(String url) {
    if (TextUtils.isEmpty(url)) {
        return;
    }
    if (!url.startsWith(HTTP) && !url.startsWith(HTTPS)) {
        url = HTTP + url;
    }
    if (null != mEditText) {
        Editable editable = mEditText.getEditableText();
        int start = mEditText.getSelectionStart();
        int end = mEditText.getSelectionEnd();
        if (start == end) {
            editable.insert(start, url);
            end = start + url.length();
        }
        editable.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
Also used : Editable(android.text.Editable) URLSpan(android.text.style.URLSpan)

Example 28 with URLSpan

use of android.text.style.URLSpan in project android_packages_apps_Settings by crdroidandroid.

the class SupportDisclaimerDialogFragment method stripUnderlines.

/**
 * Removes the underlines of {@link android.text.style.URLSpan}s.
 */
private static void stripUnderlines(Spannable input) {
    final URLSpan[] urls = input.getSpans(0, input.length(), URLSpan.class);
    for (URLSpan span : urls) {
        final int start = input.getSpanStart(span);
        final int end = input.getSpanEnd(span);
        if (!TextUtils.isEmpty(span.getURL())) {
            input.removeSpan(span);
            input.setSpan(new NoUnderlineUrlSpan(span.getURL()), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : URLSpan(android.text.style.URLSpan) TextPaint(android.text.TextPaint)

Example 29 with URLSpan

use of android.text.style.URLSpan in project android_packages_apps_Settings by SudaMod.

the class SupportDisclaimerDialogFragment method stripUnderlines.

/**
 * Removes the underlines of {@link android.text.style.URLSpan}s.
 */
private static void stripUnderlines(Spannable input) {
    final URLSpan[] urls = input.getSpans(0, input.length(), URLSpan.class);
    for (URLSpan span : urls) {
        final int start = input.getSpanStart(span);
        final int end = input.getSpanEnd(span);
        if (!TextUtils.isEmpty(span.getURL())) {
            input.removeSpan(span);
            input.setSpan(new NoUnderlineUrlSpan(span.getURL()), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : URLSpan(android.text.style.URLSpan) TextPaint(android.text.TextPaint)

Example 30 with URLSpan

use of android.text.style.URLSpan in project MarkyMark-Android by M2Mobi.

the class LinkInlineDisplayItem method create.

@Override
public Spanned create(final InlineConverter<Spanned> pInlineConverter, final LinkString pMarkDownString) {
    final Spannable spannable = SpannableUtils.createSpannable(pInlineConverter, pMarkDownString);
    spannable.setSpan(new URLSpan(pMarkDownString.getUrl()), 0, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannable;
}
Also used : URLSpan(android.text.style.URLSpan) Spannable(android.text.Spannable)

Aggregations

URLSpan (android.text.style.URLSpan)134 SpannableString (android.text.SpannableString)36 Spannable (android.text.Spannable)22 SpannableStringBuilder (android.text.SpannableStringBuilder)21 TextPaint (android.text.TextPaint)21 Spanned (android.text.Spanned)20 TextView (android.widget.TextView)19 ForegroundColorSpan (android.text.style.ForegroundColorSpan)16 StyleSpan (android.text.style.StyleSpan)16 UnderlineSpan (android.text.style.UnderlineSpan)14 Paint (android.graphics.Paint)13 ImageSpan (android.text.style.ImageSpan)13 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)13 View (android.view.View)13 Intent (android.content.Intent)12 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)12 BackgroundColorSpan (android.text.style.BackgroundColorSpan)12 ClickableSpan (android.text.style.ClickableSpan)12 SubscriptSpan (android.text.style.SubscriptSpan)12