Search in sources :

Example 61 with URLSpan

use of android.text.style.URLSpan in project BarcodeEye by BarcodeEye.

the class SupplementalInfoRetriever method append.

final void append(String itemID, String source, String[] newTexts, String linkURL) {
    StringBuilder newTextCombined = new StringBuilder();
    if (source != null) {
        newTextCombined.append(source).append(' ');
    }
    int linkStart = newTextCombined.length();
    boolean first = true;
    for (String newText : newTexts) {
        if (first) {
            newTextCombined.append(newText);
            first = false;
        } else {
            newTextCombined.append(" [");
            newTextCombined.append(newText);
            newTextCombined.append(']');
        }
    }
    int linkEnd = newTextCombined.length();
    String newText = newTextCombined.toString();
    Spannable content = new SpannableString(newText + "\n\n");
    if (linkURL != null) {
        // Lower-case these as it should always be OK to lower-case these schemes.
        if (linkURL.startsWith("HTTP://")) {
            linkURL = "http" + linkURL.substring(4);
        } else if (linkURL.startsWith("HTTPS://")) {
            linkURL = "https" + linkURL.substring(5);
        }
        content.setSpan(new URLSpan(linkURL), linkStart, linkEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    newContents.add(content);
}
Also used : SpannableString(android.text.SpannableString) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) Spannable(android.text.Spannable)

Example 62 with URLSpan

use of android.text.style.URLSpan in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SpannableStringUtilsTests method testConcatWithSuggestionSpansOnly.

public void testConcatWithSuggestionSpansOnly() {
    SpannableStringBuilder s = new SpannableStringBuilder("test string\ntest string\n" + "test string\ntest string\ntest string\ntest string\ntest string\ntest string\n" + "test string\ntest string\n");
    final int N = 10;
    for (int i = 0; i < N; ++i) {
        // Put a PARAGRAPH-flagged span that should not be found in the result.
        s.setSpan(new SuggestionSpan(getContext(), new String[] { "" + i }, Spanned.SPAN_PARAGRAPH), i * 12, i * 12 + 12, Spanned.SPAN_PARAGRAPH);
        // Put a normal suggestion span that should be found in the result.
        s.setSpan(new SuggestionSpan(getContext(), new String[] { "" + i }, 0), i, i * 2, 0);
        // Put a URL span than should not be found in the result.
        s.setSpan(new URLSpan("http://a"), i, i * 2, 0);
    }
    final CharSequence a = s.subSequence(0, 15);
    final CharSequence b = s.subSequence(15, s.length());
    final Spanned result = (Spanned) SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(a, b);
    Object[] spans = result.getSpans(0, result.length(), SuggestionSpan.class);
    for (int i = 0; i < spans.length; i++) {
        final int flags = result.getSpanFlags(spans[i]);
        assertEquals("Should not find a span with PARAGRAPH flag", flags & Spanned.SPAN_PARAGRAPH, 0);
        assertTrue("Should be a SuggestionSpan", spans[i] instanceof SuggestionSpan);
    }
}
Also used : SuggestionSpan(android.text.style.SuggestionSpan) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 63 with URLSpan

use of android.text.style.URLSpan in project BBS-Android by bdpqchen.

the class RichTextMovementMethod method onTouchEvent.

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    final Context context = widget.getContext();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();
        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);
        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
        ImageSpan[] imageSpans = buffer.getSpans(off, off, ImageSpan.class);
        if (isNotEmpty(link)) {
            if (isActionUp(action)) {
                ClickableSpan span = link[0];
                if (span instanceof URLSpan) {
                    Uri uri = Uri.parse(((URLSpan) span).getURL());
                    LogUtil.dd("uri", uri.toString());
                    if (TextUtil.isInnerLink(uri)) {
                        if (TextUtil.isThread(uri.toString())) {
                            int threadId = CastUtil.parse2intWithMin(uri.getLastPathSegment());
                            Intent intent = IntentUtil.toThread(context, threadId);
                            context.startActivity(intent);
                        }
                        if (TextUtil.isImg(uri.toString())) {
                            Intent intent = IntentUtil.toBigPhoto(context, uri.toString());
                            context.startActivity(intent);
                        }
                    } else if (TextUtil.isAtUserLink(uri)) {
                        context.startActivity(IntentUtil.toPeople(context, TextUtil.getAtUid(uri.toString())));
                    } else {
                        // 调用系统默认链接点击事件
                        if (TextUtil.isOuterLink(uri)) {
                            link[0].onClick(widget);
                        }
                    }
                }
            }
            return true;
        } else if (isImage(imageSpans)) {
            if (isActionUp(action)) {
                context.startActivity(IntentUtil.toBigPhoto(context, imageSpans[0].getSource()));
            }
            return true;
        }
        return super.onTouchEvent(widget, buffer, event);
    }
    return super.onTouchEvent(widget, buffer, event);
}
Also used : Context(android.content.Context) Layout(android.text.Layout) Intent(android.content.Intent) URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan) Uri(android.net.Uri) ImageSpan(android.text.style.ImageSpan)

Example 64 with URLSpan

use of android.text.style.URLSpan in project FBReaderJ by geometer.

the class HtmlUtil method getHtmlText.

public static CharSequence getHtmlText(NetworkLibrary library, String text) {
    final Spanned htmlText = Html.fromHtml(text);
    if (htmlText.getSpans(0, htmlText.length(), URLSpan.class).length == 0) {
        return htmlText;
    }
    final Spannable newHtmlText = Spannable.Factory.getInstance().newSpannable(htmlText);
    for (URLSpan span : newHtmlText.getSpans(0, newHtmlText.length(), URLSpan.class)) {
        final int start = newHtmlText.getSpanStart(span);
        final int end = newHtmlText.getSpanEnd(span);
        final int flags = newHtmlText.getSpanFlags(span);
        final String url = library.rewriteUrl(span.getURL(), true);
        newHtmlText.removeSpan(span);
        newHtmlText.setSpan(new URLSpan(url), start, end, flags);
    }
    return newHtmlText;
}
Also used : URLSpan(android.text.style.URLSpan)

Example 65 with URLSpan

use of android.text.style.URLSpan in project AndroidUtilCode by Blankj.

the class SpanUtils method updateCharCharSequence.

private void updateCharCharSequence() {
    if (mText.length() == 0)
        return;
    int start = mBuilder.length();
    if (start == 0 && lineHeight != -1) {
        // bug of LineHeightSpan when first line
        mBuilder.append(Character.toString((char) 2)).append("\n").setSpan(new AbsoluteSizeSpan(0), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        start = 2;
    }
    mBuilder.append(mText);
    int end = mBuilder.length();
    if (verticalAlign != -1) {
        mBuilder.setSpan(new VerticalAlignSpan(verticalAlign), start, end, flag);
    }
    if (foregroundColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new ForegroundColorSpan(foregroundColor), start, end, flag);
    }
    if (backgroundColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new BackgroundColorSpan(backgroundColor), start, end, flag);
    }
    if (first != -1) {
        mBuilder.setSpan(new LeadingMarginSpan.Standard(first, rest), start, end, flag);
    }
    if (quoteColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new CustomQuoteSpan(quoteColor, stripeWidth, quoteGapWidth), start, end, flag);
    }
    if (bulletColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new CustomBulletSpan(bulletColor, bulletRadius, bulletGapWidth), start, end, flag);
    }
    if (fontSize != -1) {
        mBuilder.setSpan(new AbsoluteSizeSpan(fontSize, false), start, end, flag);
    }
    if (proportion != -1) {
        mBuilder.setSpan(new RelativeSizeSpan(proportion), start, end, flag);
    }
    if (xProportion != -1) {
        mBuilder.setSpan(new ScaleXSpan(xProportion), start, end, flag);
    }
    if (lineHeight != -1) {
        mBuilder.setSpan(new CustomLineHeightSpan(lineHeight, alignLine), start, end, flag);
    }
    if (isStrikethrough) {
        mBuilder.setSpan(new StrikethroughSpan(), start, end, flag);
    }
    if (isUnderline) {
        mBuilder.setSpan(new UnderlineSpan(), start, end, flag);
    }
    if (isSuperscript) {
        mBuilder.setSpan(new SuperscriptSpan(), start, end, flag);
    }
    if (isSubscript) {
        mBuilder.setSpan(new SubscriptSpan(), start, end, flag);
    }
    if (isBold) {
        mBuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, flag);
    }
    if (isItalic) {
        mBuilder.setSpan(new StyleSpan(Typeface.ITALIC), start, end, flag);
    }
    if (isBoldItalic) {
        mBuilder.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, end, flag);
    }
    if (fontFamily != null) {
        mBuilder.setSpan(new TypefaceSpan(fontFamily), start, end, flag);
    }
    if (typeface != null) {
        mBuilder.setSpan(new CustomTypefaceSpan(typeface), start, end, flag);
    }
    if (alignment != null) {
        mBuilder.setSpan(new AlignmentSpan.Standard(alignment), start, end, flag);
    }
    if (clickSpan != null) {
        mBuilder.setSpan(clickSpan, start, end, flag);
    }
    if (url != null) {
        mBuilder.setSpan(new URLSpan(url), start, end, flag);
    }
    if (blurRadius != -1) {
        mBuilder.setSpan(new MaskFilterSpan(new BlurMaskFilter(blurRadius, style)), start, end, flag);
    }
    if (shader != null) {
        mBuilder.setSpan(new ShaderSpan(shader), start, end, flag);
    }
    if (shadowRadius != -1) {
        mBuilder.setSpan(new ShadowSpan(shadowRadius, shadowDx, shadowDy, shadowColor), start, end, flag);
    }
    if (spans != null) {
        for (Object span : spans) {
            mBuilder.setSpan(span, start, end, flag);
        }
    }
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) SuperscriptSpan(android.text.style.SuperscriptSpan) ScaleXSpan(android.text.style.ScaleXSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) SubscriptSpan(android.text.style.SubscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) MaskFilterSpan(android.text.style.MaskFilterSpan) StyleSpan(android.text.style.StyleSpan) LeadingMarginSpan(android.text.style.LeadingMarginSpan) BlurMaskFilter(android.graphics.BlurMaskFilter) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) TypefaceSpan(android.text.style.TypefaceSpan)

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