Search in sources :

Example 11 with TypefaceSpan

use of android.text.style.TypefaceSpan in project AndroidChromium by JackyAndroid.

the class ExpandablePreferenceGroup method setGroupTitle.

/**
     * Set the title for the preference group.
     * @param resourceId The resource id of the text to use.
     * @param count The number of entries the preference group contains.
     */
public void setGroupTitle(int resourceId, int count) {
    SpannableStringBuilder spannable = new SpannableStringBuilder(getContext().getResources().getString(resourceId));
    String prefCount = String.format(Locale.getDefault(), " - %d", count);
    spannable.append(prefCount);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, spannable.length() - prefCount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        spannable.setSpan(new TypefaceSpan("sans-serif-medium"), 0, spannable.length() - prefCount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    // Color the first part of the title blue.
    ForegroundColorSpan blueSpan = new ForegroundColorSpan(ApiCompatibilityUtils.getColor(getContext().getResources(), R.color.pref_accent_color));
    spannable.setSpan(blueSpan, 0, spannable.length() - prefCount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    // Gray out the total count of items.
    int gray = ApiCompatibilityUtils.getColor(getContext().getResources(), R.color.expandable_group_dark_gray);
    spannable.setSpan(new ForegroundColorSpan(gray), spannable.length() - prefCount.length(), spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    setTitle(spannable);
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) TypefaceSpan(android.text.style.TypefaceSpan)

Example 12 with TypefaceSpan

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

the class HtmlTagHandler method handleTag.

@Override
public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
    if (opening) {
        // opening tag
        if (HtmlTextView.DEBUG) {
            Log.d(HtmlTextView.TAG, "opening, output: " + output.toString());
        }
        if (tag.equalsIgnoreCase(UNORDERED_LIST)) {
            lists.push(tag);
        } else if (tag.equalsIgnoreCase(ORDERED_LIST)) {
            lists.push(tag);
            olNextIndex.push(1);
        } else if (tag.equalsIgnoreCase(LIST_ITEM)) {
            if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                output.append("\n");
            }
            if (!lists.isEmpty()) {
                String parentList = lists.peek();
                if (parentList.equalsIgnoreCase(ORDERED_LIST)) {
                    start(output, new Ol());
                    olNextIndex.push(olNextIndex.pop() + 1);
                } else if (parentList.equalsIgnoreCase(UNORDERED_LIST)) {
                    start(output, new Ul());
                }
            }
        } else if (tag.equalsIgnoreCase("code")) {
            start(output, new Code());
        } else if (tag.equalsIgnoreCase("center")) {
            start(output, new Center());
        } else if (tag.equalsIgnoreCase("s") || tag.equalsIgnoreCase("strike")) {
            start(output, new Strike());
        } else if (tag.equalsIgnoreCase("table")) {
            start(output, new Table());
            if (tableTagLevel == 0) {
                tableHtmlBuilder = new StringBuilder();
                // We need some text for the table to be replaced by the span because
                // the other tags will remove their text when their text is extracted
                output.append("table placeholder");
            }
            tableTagLevel++;
        } else if (tag.equalsIgnoreCase("tr")) {
            start(output, new Tr());
        } else if (tag.equalsIgnoreCase("th")) {
            start(output, new Th());
        } else if (tag.equalsIgnoreCase("td")) {
            start(output, new Td());
        }
    } else {
        // closing tag
        if (HtmlTextView.DEBUG) {
            Log.d(HtmlTextView.TAG, "closing, output: " + output.toString());
        }
        if (tag.equalsIgnoreCase(UNORDERED_LIST)) {
            lists.pop();
        } else if (tag.equalsIgnoreCase(ORDERED_LIST)) {
            lists.pop();
            olNextIndex.pop();
        } else if (tag.equalsIgnoreCase(LIST_ITEM)) {
            if (!lists.isEmpty()) {
                if (lists.peek().equalsIgnoreCase(UNORDERED_LIST)) {
                    if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                        output.append("\n");
                    }
                    // Nested BulletSpans increases distance between bullet and text, so we must prevent it.
                    int bulletMargin = indent;
                    if (lists.size() > 1) {
                        bulletMargin = indent - bullet.getLeadingMargin(true);
                        if (lists.size() > 2) {
                            // This get's more complicated when we add a LeadingMarginSpan into the same line:
                            // we have also counter it's effect to BulletSpan
                            bulletMargin -= (lists.size() - 2) * listItemIndent;
                        }
                    }
                    BulletSpan newBullet = new BulletSpan(bulletMargin);
                    end(output, Ul.class, false, new LeadingMarginSpan.Standard(listItemIndent * (lists.size() - 1)), newBullet);
                } else if (lists.peek().equalsIgnoreCase(ORDERED_LIST)) {
                    if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
                        output.append("\n");
                    }
                    int numberMargin = listItemIndent * (lists.size() - 1);
                    if (lists.size() > 2) {
                        // Same as in ordered lists: counter the effect of nested Spans
                        numberMargin -= (lists.size() - 2) * listItemIndent;
                    }
                    NumberSpan numberSpan = new NumberSpan(mTextPaint, olNextIndex.lastElement() - 1);
                    end(output, Ol.class, false, new LeadingMarginSpan.Standard(numberMargin), numberSpan);
                }
            }
        } else if (tag.equalsIgnoreCase("code")) {
            end(output, Code.class, false, new TypefaceSpan("monospace"));
        } else if (tag.equalsIgnoreCase("center")) {
            end(output, Center.class, true, new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER));
        } else if (tag.equalsIgnoreCase("s") || tag.equalsIgnoreCase("strike")) {
            end(output, Strike.class, false, new StrikethroughSpan());
        } else if (tag.equalsIgnoreCase("table")) {
            tableTagLevel--;
            // When we're back at the root-level table
            if (tableTagLevel == 0) {
                final String tableHtml = tableHtmlBuilder.toString();
                ClickableTableSpan myClickableTableSpan = null;
                if (clickableTableSpan != null) {
                    myClickableTableSpan = clickableTableSpan.newInstance();
                    myClickableTableSpan.setTableHtml(tableHtml);
                }
                DrawTableLinkSpan myDrawTableLinkSpan = null;
                if (drawTableLinkSpan != null) {
                    myDrawTableLinkSpan = drawTableLinkSpan.newInstance();
                }
                end(output, Table.class, false, myDrawTableLinkSpan, myClickableTableSpan);
            } else {
                end(output, Table.class, false);
            }
        } else if (tag.equalsIgnoreCase("tr")) {
            end(output, Tr.class, false);
        } else if (tag.equalsIgnoreCase("th")) {
            end(output, Th.class, false);
        } else if (tag.equalsIgnoreCase("td")) {
            end(output, Td.class, false);
        }
    }
    storeTableTags(opening, tag);
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) TextPaint(android.text.TextPaint) BulletSpan(android.text.style.BulletSpan) LeadingMarginSpan(android.text.style.LeadingMarginSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 13 with TypefaceSpan

use of android.text.style.TypefaceSpan in project plaid by nickbutcher.

the class Bypass method recurseElement.

// The 'numberOfSiblings' parameters refers to the number of siblings within the parent, including
// the 'element' parameter, as in "How many siblings are you?" rather than "How many siblings do
// you have?".
private CharSequence recurseElement(Element element, int indexWithinParent, int numberOfSiblings, TextView textView, LoadImageCallback loadImageCallback) {
    Type type = element.getType();
    boolean isOrderedList = false;
    if (type == Type.LIST) {
        String flagsStr = element.getAttribute("flags");
        if (flagsStr != null) {
            int flags = Integer.parseInt(flagsStr);
            isOrderedList = (flags & Element.F_LIST_ORDERED) != 0;
            if (isOrderedList) {
                mOrderedListNumber.put(element, 1);
            }
        }
    }
    int size = element.size();
    CharSequence[] spans = new CharSequence[size];
    for (int i = 0; i < size; i++) {
        spans[i] = recurseElement(element.children[i], i, size, textView, loadImageCallback);
    }
    // Clean up after we're done
    if (isOrderedList) {
        mOrderedListNumber.remove(this);
    }
    CharSequence concat = TextUtils.concat(spans);
    SpannableStringBuilder builder = new ReverseSpannableStringBuilder();
    String text = element.getText();
    if (element.size() == 0 && element.getParent() != null && element.getParent().getType() != Type.BLOCK_CODE) {
        text = text.replace('\n', ' ');
    }
    switch(type) {
        case LIST:
            if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
                builder.append("\n");
            }
            break;
        case LINEBREAK:
            builder.append("\n");
            break;
        case LIST_ITEM:
            builder.append(" ");
            if (mOrderedListNumber.containsKey(element.getParent())) {
                int number = mOrderedListNumber.get(element.getParent());
                builder.append(Integer.toString(number) + ".");
                mOrderedListNumber.put(element.getParent(), number + 1);
            } else {
                builder.append(mOptions.mUnorderedListItem);
            }
            builder.append("  ");
            break;
        case AUTOLINK:
            builder.append(element.getAttribute("link"));
            break;
        case HRULE:
            // This ultimately gets drawn over by the line span, but
            // we need something here or the span isn't even drawn.
            builder.append("-");
            break;
        case IMAGE:
            if (loadImageCallback != null && !TextUtils.isEmpty(element.getAttribute("link"))) {
                // prepend a new line so that images are always on a new line
                builder.append("\n");
                // Display alt text (or title text) if there is no image
                String alt = element.getAttribute("alt");
                if (TextUtils.isEmpty(alt)) {
                    alt = element.getAttribute("title");
                }
                if (!TextUtils.isEmpty(alt)) {
                    alt = "[" + alt + "]";
                    builder.append(alt);
                } else {
                    // Character to be replaced
                    builder.append("");
                }
            } else {
                // Character to be replaced
                builder.append("");
            }
            break;
    }
    builder.append(text);
    builder.append(concat);
    // of the last child within the parent.
    if (element.getParent() != null || indexWithinParent < (numberOfSiblings - 1)) {
        if (type == Type.LIST_ITEM) {
            if (element.size() == 0 || !element.children[element.size() - 1].isBlockElement()) {
                builder.append("\n");
            }
        } else if (element.isBlockElement() && type != Type.BLOCK_QUOTE) {
            if (type == Type.LIST) {
                // If this is a nested list, don't include newlines
                if (element.getParent() == null || element.getParent().getType() != Type.LIST_ITEM) {
                    builder.append("\n");
                }
            } else if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
                // List items should never double-space their entries
                builder.append("\n");
            } else {
                builder.append("\n\n");
            }
        }
    }
    switch(type) {
        case HEADER:
            String levelStr = element.getAttribute("level");
            int level = Integer.parseInt(levelStr);
            setSpan(builder, new RelativeSizeSpan(mOptions.mHeaderSizes[level - 1]));
            setSpan(builder, new StyleSpan(Typeface.BOLD));
            break;
        case LIST:
            setBlockSpan(builder, new LeadingMarginSpan.Standard(mListItemIndent));
            break;
        case EMPHASIS:
            setSpan(builder, new StyleSpan(Typeface.ITALIC));
            break;
        case DOUBLE_EMPHASIS:
            setSpan(builder, new StyleSpan(Typeface.BOLD));
            break;
        case TRIPLE_EMPHASIS:
            setSpan(builder, new StyleSpan(Typeface.BOLD_ITALIC));
            break;
        case BLOCK_CODE:
            setSpan(builder, new LeadingMarginSpan.Standard(mCodeBlockIndent));
            setSpan(builder, new TypefaceSpan("monospace"));
            break;
        case CODE_SPAN:
            setSpan(builder, new TypefaceSpan("monospace"));
            break;
        case LINK:
        case AUTOLINK:
            String link = element.getAttribute("link");
            if (!TextUtils.isEmpty(link) && Patterns.EMAIL_ADDRESS.matcher(link).matches()) {
                link = "mailto:" + link;
            }
            setSpan(builder, new TouchableUrlSpan(link, textView.getLinkTextColors(), textView.getHighlightColor()));
            break;
        case BLOCK_QUOTE:
            // We add two leading margin spans so that when the order is reversed,
            // the QuoteSpan will always be in the same spot.
            setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
            //setBlockSpan(builder, new QuoteSpan(mOptions.mBlockQuoteLineColor));
            setBlockSpan(builder, new FancyQuoteSpan(mBlockQuoteLineWidth, mBlockQuoteLineIndent, mOptions.mBlockQuoteLineColor));
            setBlockSpan(builder, new ForegroundColorSpan(mOptions.mBlockQuoteTextColor));
            setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
            setBlockSpan(builder, new StyleSpan(Typeface.ITALIC));
            break;
        case STRIKETHROUGH:
            setSpan(builder, new StrikethroughSpan());
            break;
        case HRULE:
            setSpan(builder, new HorizontalLineSpan(mOptions.mHruleColor, mHruleSize, mHruleTopBottomPadding));
            break;
        case IMAGE:
            String url = element.getAttribute("link");
            if (loadImageCallback != null && !TextUtils.isEmpty(url)) {
                setPrependedNewlineSpan(builder, mOptions.mPreImageLinebreakHeight);
                ImageLoadingSpan loadingSpan = new ImageLoadingSpan();
                setSpanWithPrependedNewline(builder, loadingSpan);
                // make the (eventually loaded) image span clickable to open in browser
                setSpanWithPrependedNewline(builder, new TouchableUrlSpan(url, textView.getLinkTextColors(), textView.getHighlightColor()));
                loadImageCallback.loadImage(url, loadingSpan);
            }
            break;
    }
    return builder;
}
Also used : TouchableUrlSpan(in.uncod.android.bypass.style.TouchableUrlSpan) HorizontalLineSpan(in.uncod.android.bypass.style.HorizontalLineSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) FancyQuoteSpan(in.uncod.android.bypass.style.FancyQuoteSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) Type(in.uncod.android.bypass.Element.Type) StyleSpan(android.text.style.StyleSpan) LeadingMarginSpan(android.text.style.LeadingMarginSpan) ImageLoadingSpan(in.uncod.android.bypass.style.ImageLoadingSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 14 with TypefaceSpan

use of android.text.style.TypefaceSpan in project android_frameworks_base by ParanoidAndroid.

the class HtmlToSpannedConverter method endFont.

private static void endFont(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Font.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);
    if (where != len) {
        Font f = (Font) obj;
        if (!TextUtils.isEmpty(f.mColor)) {
            if (f.mColor.startsWith("@")) {
                Resources res = Resources.getSystem();
                String name = f.mColor.substring(1);
                int colorRes = res.getIdentifier(name, "color", "android");
                if (colorRes != 0) {
                    ColorStateList colors = res.getColorStateList(colorRes);
                    text.setSpan(new TextAppearanceSpan(null, 0, 0, colors, null), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            } else {
                int c = Color.getHtmlColor(f.mColor);
                if (c != -1) {
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        if (f.mFace != null) {
            text.setSpan(new TypefaceSpan(f.mFace), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) ColorStateList(android.content.res.ColorStateList) Resources(android.content.res.Resources) TypefaceSpan(android.text.style.TypefaceSpan)

Example 15 with TypefaceSpan

use of android.text.style.TypefaceSpan in project android_frameworks_base by ParanoidAndroid.

the class HtmlTest method testMarkup.

@SmallTest
public void testMarkup() throws Exception {
    SpannableString s;
    s = new SpannableString("Hello bold world");
    s.setSpan(new StyleSpan(Typeface.BOLD), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <b>bold</b> world</p>\n");
    s = new SpannableString("Hello italic world");
    s.setSpan(new StyleSpan(Typeface.ITALIC), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <i>italic</i> world</p>\n");
    s = new SpannableString("Hello monospace world");
    s.setSpan(new TypefaceSpan("monospace"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <tt>monospace</tt> world</p>\n");
    s = new SpannableString("Hello superscript world");
    s.setSpan(new SuperscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <sup>superscript</sup> world</p>\n");
    s = new SpannableString("Hello subscript world");
    s.setSpan(new SubscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <sub>subscript</sub> world</p>\n");
    s = new SpannableString("Hello underline world");
    s.setSpan(new UnderlineSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <u>underline</u> world</p>\n");
    s = new SpannableString("Hello struck world");
    s.setSpan(new StrikethroughSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <strike>struck</strike> world</p>\n");
    s = new SpannableString("Hello linky world");
    s.setSpan(new URLSpan("http://www.google.com"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    assertEquals(Html.toHtml(s), "<p>Hello <a href=\"http://www.google.com\">linky</a> world</p>\n");
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) URLSpan(android.text.style.URLSpan) UnderlineSpan(android.text.style.UnderlineSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

TypefaceSpan (android.text.style.TypefaceSpan)25 ForegroundColorSpan (android.text.style.ForegroundColorSpan)16 StyleSpan (android.text.style.StyleSpan)12 RelativeSizeSpan (android.text.style.RelativeSizeSpan)10 StrikethroughSpan (android.text.style.StrikethroughSpan)10 SubscriptSpan (android.text.style.SubscriptSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)10 UnderlineSpan (android.text.style.UnderlineSpan)8 URLSpan (android.text.style.URLSpan)7 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)6 CharacterStyle (android.text.style.CharacterStyle)6 Application (android.app.Application)5 SpannableString (android.text.SpannableString)5 BackgroundColorSpan (android.text.style.BackgroundColorSpan)5 ImageSpan (android.text.style.ImageSpan)5 ColorStateList (android.content.res.ColorStateList)3 Resources (android.content.res.Resources)3 QuoteSpan (android.text.style.QuoteSpan)3 TextAppearanceSpan (android.text.style.TextAppearanceSpan)3 AlertDialog (android.app.AlertDialog)2