Search in sources :

Example 46 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project android_frameworks_base by ResurrectionRemix.

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 AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.getResources().getDisplayMetrics().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) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 47 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project Tusky by Vavassor.

the class SpanUtils method highlightSpans.

public static void highlightSpans(Spannable text, int colour) {
    // Strip all existing colour spans.
    int n = text.length();
    ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
    for (int i = oldSpans.length - 1; i >= 0; i--) {
        text.removeSpan(oldSpans[i]);
    }
    // Colour the mentions and hashtags.
    String string = text.toString();
    int start;
    int end = 0;
    while (end < n) {
        char[] chars = { '#', '@' };
        FindCharsResult found = findStart(string, end, chars);
        start = found.stringIndex;
        if (start < 0) {
            break;
        }
        if (found.charIndex == 0) {
            end = findEndOfHashtag(string, start);
        } else if (found.charIndex == 1) {
            end = findEndOfMention(string, start);
        } else {
            break;
        }
        if (end < 0) {
            break;
        }
        text.setSpan(new ForegroundColorSpan(colour), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan)

Example 48 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project android_frameworks_base by ResurrectionRemix.

the class ViewPropertyAlphaActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_properties);
    getWindow().getDecorView().postDelayed(new Runnable() {

        @Override
        public void run() {
            startAnim(R.id.button);
            startAnim(R.id.textview);
            startAnim(R.id.spantext);
            startAnim(R.id.edittext);
            startAnim(R.id.selectedtext);
            startAnim(R.id.textviewbackground);
            startAnim(R.id.layout);
            startAnim(R.id.imageview);
            startAnim(myViewAlphaDefault);
            startAnim(myViewAlphaHandled);
            EditText selectedText = (EditText) findViewById(R.id.selectedtext);
            selectedText.setSelection(3, 8);
        }
    }, 2000);
    Button invalidator = (Button) findViewById(R.id.invalidateButton);
    invalidator.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.textview).invalidate();
            findViewById(R.id.spantext).invalidate();
        }
    });
    TextView textView = (TextView) findViewById(R.id.spantext);
    if (textView != null) {
        SpannableStringBuilder text = new SpannableStringBuilder("Now this is a short text message with spans");
        text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new SuggestionSpan(this, new String[] { "longer" }, 3), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new UnderlineSpan(), 17, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(text);
    }
    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    myViewAlphaDefault = new MyView(this, false);
    myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaDefault);
    myViewAlphaHandled = new MyView(this, true);
    myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaHandled);
}
Also used : EditText(android.widget.EditText) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextView(android.widget.TextView) View(android.view.View) UnderlineSpan(android.text.style.UnderlineSpan) Button(android.widget.Button) TextView(android.widget.TextView) SuggestionSpan(android.text.style.SuggestionSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan) LinearLayout(android.widget.LinearLayout) ImageSpan(android.text.style.ImageSpan)

Example 49 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ChartDataUsagePreference method getLabel.

private CharSequence getLabel(long bytes, int str, int mLimitColor) {
    Formatter.BytesResult result = Formatter.formatBytes(getContext().getResources(), bytes, Formatter.FLAG_SHORTER);
    CharSequence label = TextUtils.expandTemplate(getContext().getText(str), result.value, result.units);
    return new SpannableStringBuilder().append(label, new ForegroundColorSpan(mLimitColor), 0);
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) Formatter(android.text.format.Formatter) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 50 with ForegroundColorSpan

use of android.text.style.ForegroundColorSpan in project bilibili-android-client by HotBitmapGG.

the class HomeRecommendedSection method onBindHeaderViewHolder.

@SuppressLint("SetTextI18n")
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
    HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
    setTypeIcon(headerViewHolder);
    headerViewHolder.mTypeTv.setText(title);
    headerViewHolder.mTypeRankBtn.setOnClickListener(v -> mContext.startActivity(new Intent(mContext, OriginalRankActivity.class)));
    switch(type) {
        case TYPE_RECOMMENDED:
            headerViewHolder.mTypeMore.setVisibility(View.GONE);
            headerViewHolder.mTypeRankBtn.setVisibility(View.VISIBLE);
            headerViewHolder.mAllLiveNum.setVisibility(View.GONE);
            break;
        case TYPE_LIVE:
            headerViewHolder.mTypeRankBtn.setVisibility(View.GONE);
            headerViewHolder.mTypeMore.setVisibility(View.VISIBLE);
            headerViewHolder.mAllLiveNum.setVisibility(View.VISIBLE);
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder("当前" + liveCount + "个直播");
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.pink_text_color));
            stringBuilder.setSpan(foregroundColorSpan, 2, stringBuilder.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            headerViewHolder.mAllLiveNum.setText(stringBuilder);
            break;
        default:
            headerViewHolder.mTypeRankBtn.setVisibility(View.GONE);
            headerViewHolder.mTypeMore.setVisibility(View.VISIBLE);
            headerViewHolder.mAllLiveNum.setVisibility(View.GONE);
            break;
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) Intent(android.content.Intent) SpannableStringBuilder(android.text.SpannableStringBuilder) SuppressLint(android.annotation.SuppressLint)

Aggregations

ForegroundColorSpan (android.text.style.ForegroundColorSpan)157 SpannableStringBuilder (android.text.SpannableStringBuilder)57 SpannableString (android.text.SpannableString)50 StyleSpan (android.text.style.StyleSpan)25 TextView (android.widget.TextView)25 ImageSpan (android.text.style.ImageSpan)23 Spannable (android.text.Spannable)22 RelativeSizeSpan (android.text.style.RelativeSizeSpan)22 View (android.view.View)22 BackgroundColorSpan (android.text.style.BackgroundColorSpan)19 TypefaceSpan (android.text.style.TypefaceSpan)16 StrikethroughSpan (android.text.style.StrikethroughSpan)14 UnderlineSpan (android.text.style.UnderlineSpan)13 Drawable (android.graphics.drawable.Drawable)12 CharacterStyle (android.text.style.CharacterStyle)11 EditText (android.widget.EditText)11 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)8 LinearLayout (android.widget.LinearLayout)8 URLSpan (android.text.style.URLSpan)7