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);
}
}
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations