Search in sources :

Example 51 with CharacterStyle

use of android.text.style.CharacterStyle in project PhoneProfiles by henrichg.

the class ApplicationsDialogPreferenceViewHolder method setTextStyle.

private void setTextStyle(TextView textView, boolean errorColor) {
    if (textView != null) {
        CharSequence title = textView.getText();
        Spannable sbt = new SpannableString(title);
        Object[] spansToRemove = sbt.getSpans(0, title.length(), Object.class);
        for (Object span : spansToRemove) {
            if (span instanceof CharacterStyle)
                sbt.removeSpan(span);
        }
        if (errorColor) {
            sbt.setSpan(new ForegroundColorSpan(Color.RED), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(sbt);
        } else {
            textView.setText(sbt);
        }
    }
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Spannable(android.text.Spannable) CharacterStyle(android.text.style.CharacterStyle)

Example 52 with CharacterStyle

use of android.text.style.CharacterStyle in project XRichText by sendtion.

the class StringUtils method highlight.

/**
 * 关键字高亮显示
 * @param target  需要高亮的关键字
 * @param text	     需要显示的文字
 * @return spannable 处理完后的结果,记得不要toString(),否则没有效果
 * SpannableStringBuilder textString = TextUtilTools.highlight(item.getItemName(), KnowledgeActivity.searchKey);
 * vHolder.tv_itemName_search.setText(textString);
 */
public static SpannableStringBuilder highlight(String text, String target) {
    SpannableStringBuilder spannable = new SpannableStringBuilder(text);
    CharacterStyle span = null;
    Pattern p = Pattern.compile(target);
    Matcher m = p.matcher(text);
    while (m.find()) {
        // 需要重复!
        span = new ForegroundColorSpan(Color.parseColor("#EE5C42"));
        spannable.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spannable;
}
Also used : Pattern(java.util.regex.Pattern) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Matcher(java.util.regex.Matcher) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 53 with CharacterStyle

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

the class AREditText method onSelectionChanged.

/*
	 * ----------------------------------------- * Rich Text Style Area
	 * -----------------------------------------
	 */
@Override
public void onSelectionChanged(int selStart, int selEnd) {
    if (sToolbar == null) {
        return;
    }
    Util.log(" on Selection changed == " + this);
    boolean boldExists = false;
    boolean italicsExists = false;
    boolean underlinedExists = false;
    boolean striketrhoughExists = false;
    boolean backgroundColorExists = false;
    boolean quoteExists = false;
    // 
    // Two cases:
    // 1. Selection is just a pure cursor
    // 2. Selection is a range
    Editable editable = this.getEditableText();
    if (selStart > 0 && selStart == selEnd) {
        CharacterStyle[] styleSpans = editable.getSpans(selStart - 1, selStart, CharacterStyle.class);
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i] instanceof StyleSpan) {
                if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD) {
                    boldExists = true;
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.ITALIC) {
                    italicsExists = true;
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD_ITALIC) {
                // TODO
                }
            } else if (styleSpans[i] instanceof AreUnderlineSpan) {
                underlinedExists = true;
            } else if (styleSpans[i] instanceof StrikethroughSpan) {
                striketrhoughExists = true;
            } else if (styleSpans[i] instanceof BackgroundColorSpan) {
                backgroundColorExists = true;
            }
        }
        QuoteSpan[] quoteSpans = editable.getSpans(selStart - 1, selStart, QuoteSpan.class);
        if (quoteSpans != null && quoteSpans.length > 0) {
            quoteExists = true;
        }
    // To check Quote!!
    } else {
        // 
        // Selection is a range
        CharacterStyle[] styleSpans = editable.getSpans(selStart, selEnd, CharacterStyle.class);
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i] instanceof StyleSpan) {
                if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        boldExists = true;
                    }
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.ITALIC) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        italicsExists = true;
                    }
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD_ITALIC) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        italicsExists = true;
                        boldExists = true;
                    }
                }
            } else if (styleSpans[i] instanceof AreUnderlineSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    underlinedExists = true;
                }
            } else if (styleSpans[i] instanceof StrikethroughSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    striketrhoughExists = true;
                }
            } else if (styleSpans[i] instanceof BackgroundColorSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    backgroundColorExists = true;
                }
            }
        }
    }
    QuoteSpan[] quoteSpans = editable.getSpans(selStart, selEnd, QuoteSpan.class);
    if (quoteSpans != null && quoteSpans.length > 0) {
        if (editable.getSpanStart(quoteSpans[0]) <= selStart && editable.getSpanEnd(quoteSpans[0]) >= selEnd) {
            quoteExists = true;
        }
    }
    // 
    // Set style checked status
    ARE_Helper.updateCheckStatus(sToolbar.getBoldStyle(), boldExists);
    ARE_Helper.updateCheckStatus(sToolbar.getItalicStyle(), italicsExists);
    ARE_Helper.updateCheckStatus(sToolbar.getUnderlineStyle(), underlinedExists);
    ARE_Helper.updateCheckStatus(sToolbar.getStrikethroughStyle(), striketrhoughExists);
    ARE_Helper.updateCheckStatus(sToolbar.getBackgroundColoStyle(), backgroundColorExists);
    ARE_Helper.updateCheckStatus(sToolbar.getQuoteStyle(), quoteExists);
}
Also used : AreUnderlineSpan(com.chinalwb.are.spans.AreUnderlineSpan) StyleSpan(android.text.style.StyleSpan) Editable(android.text.Editable) QuoteSpan(android.text.style.QuoteSpan) CharacterStyle(android.text.style.CharacterStyle) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 54 with CharacterStyle

use of android.text.style.CharacterStyle in project fdroidclient by f-droid.

the class Utils method formatAppNameAndSummary.

/**
 * Formats the app name using "sans-serif" and then appends the summary after a space with
 * "sans-serif-light". Doesn't mandate any font sizes or any other styles, that is up to the
 * {@link android.widget.TextView} which it ends up being displayed in.
 */
public static CharSequence formatAppNameAndSummary(String appName, String summary) {
    String toFormat = appName + ' ' + summary;
    CharacterStyle normal = new TypefaceSpan("sans-serif");
    CharacterStyle light = new TypefaceSpan("sans-serif-light");
    SpannableStringBuilder sb = new SpannableStringBuilder(toFormat);
    sb.setSpan(normal, 0, appName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.setSpan(light, appName.length(), toFormat.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}
Also used : CharacterStyle(android.text.style.CharacterStyle) SpannableStringBuilder(android.text.SpannableStringBuilder) TypefaceSpan(android.text.style.TypefaceSpan)

Example 55 with CharacterStyle

use of android.text.style.CharacterStyle in project FastHub by k0shk0sh.

the class LabelSpan method setupFontMetrics.

private void setupFontMetrics(CharSequence text, int start, int end, FontMetricsInt fm, Paint p) {
    txtPaint.set(p);
    final CharacterStyle[] otherSpans = ((Spanned) text).getSpans(start, end, CharacterStyle.class);
    for (CharacterStyle otherSpan : otherSpans) {
        otherSpan.updateDrawState(txtPaint);
    }
    txtPaint.setTextSize(p.getTextSize());
    if (fm != null) {
        txtPaint.getFontMetricsInt(fm);
    }
}
Also used : Spanned(android.text.Spanned) CharacterStyle(android.text.style.CharacterStyle)

Aggregations

CharacterStyle (android.text.style.CharacterStyle)58 SpannableStringBuilder (android.text.SpannableStringBuilder)22 ForegroundColorSpan (android.text.style.ForegroundColorSpan)18 RelativeSizeSpan (android.text.style.RelativeSizeSpan)18 Paint (android.graphics.Paint)16 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)14 StyleSpan (android.text.style.StyleSpan)13 TextPaint (android.text.TextPaint)12 ParcelableSpan (android.text.ParcelableSpan)9 SpannableString (android.text.SpannableString)9 Spanned (android.text.Spanned)9 StrikethroughSpan (android.text.style.StrikethroughSpan)9 TypefaceSpan (android.text.style.TypefaceSpan)9 UnderlineSpan (android.text.style.UnderlineSpan)9 BackgroundColorSpan (android.text.style.BackgroundColorSpan)8 ReplacementSpan (android.text.style.ReplacementSpan)8 SubscriptSpan (android.text.style.SubscriptSpan)8 SuperscriptSpan (android.text.style.SuperscriptSpan)8 URLSpan (android.text.style.URLSpan)8 ImageSpan (android.text.style.ImageSpan)7