本文整理汇总了Java中android.text.style.CharacterStyle类的典型用法代码示例。如果您正苦于以下问题:Java CharacterStyle类的具体用法?Java CharacterStyle怎么用?Java CharacterStyle使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CharacterStyle类属于android.text.style包,在下文中一共展示了CharacterStyle类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invalidateSpannables
import android.text.style.CharacterStyle; //导入依赖的package包/类
public void invalidateSpannables() {
log("invalidating all spannables -- consider everything nullified");
final Spannable spans = getText();
final String text = spans.toString();
// Remove existing spans
for (CharacterStyle style : mSpans) {
spans.removeSpan(style);
}
// Loop over the text, looking for new spans
for (int i = 0; i < text.length(); i++) {
for (SpanComponent component : mComponents) {
String equation = component.parse(text.substring(i));
if (equation != null) {
MathSpannable span = component.getSpan(equation);
spans.setSpan(span, i, i + equation.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
i += equation.length();
break;
}
}
}
setSelection(getSelectionStart());
}
示例2: setSpanBetweenTokens
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* Given either a Spannable String or a regular String and a token, apply
* the given CharacterStyle to the span between the tokens.
* <p>
* NOTE: This method was adapted from:
* http://www.androidengineer.com/2010/08/easy-method-for-formatting-android.html
* <p>
* <p>
* For example, {@code setSpanBetweenTokens("Hello ##world##!", "##", new
* ForegroundColorSpan(0xFFFF0000));} will return a CharSequence {@code
* "Hello world!"} with {@code world} in red.
*/
private CharSequence setSpanBetweenTokens(CharSequence text, String token,
CharacterStyle... cs) {
// Start and end refer to the points where the span will apply
int tokenLen = token.length();
int start = text.toString().indexOf(token) + tokenLen;
int end = text.toString().indexOf(token, start);
if (start > -1 && end > -1) {
// Copy the spannable string to a mutable spannable string
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
for (CharacterStyle c : cs)
ssb.setSpan(c, start, end, 0);
text = ssb;
}
return text;
}
示例3: setBold
import android.text.style.CharacterStyle; //导入依赖的package包/类
void setBold(boolean isBold) {
int index = getSelectionIndex();
if (index >= 0 && index < mSections.size()) {
mSections.get(index).setBold(isBold);
}
Editable edit = getEditableText();
int star = getSectionStart();
int end = getSectionEnd();
if (isBold) {
edit.setSpan(new StyleSpan(Typeface.BOLD),
star,
end,
Typeface.BOLD);
} else {
StyleSpan[] styleSpans = edit.getSpans(star,
end, StyleSpan.class);
for (CharacterStyle span : styleSpans) {
if (span instanceof StyleSpan && ((StyleSpan) span).getStyle() == Typeface.BOLD)
edit.removeSpan(span);
}
}
}
示例4: setItalic
import android.text.style.CharacterStyle; //导入依赖的package包/类
void setItalic(boolean isItalic) {
int index = getSelectionIndex();
if (index >= 0 && index < mSections.size()) {
mSections.get(index).setItalic(isItalic);
}
Editable edit = getEditableText();
int star = getSectionStart();
int end = getSectionEnd();
if (isItalic) {
edit.setSpan(new StyleSpan(Typeface.ITALIC),
star,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
StyleSpan[] styleSpans = edit.getSpans(star,
end, StyleSpan.class);
for (CharacterStyle span : styleSpans) {
if (span instanceof StyleSpan && ((StyleSpan) span).getStyle() == Typeface.ITALIC)
edit.removeSpan(span);
}
}
}
示例5: replaceAll
import android.text.style.CharacterStyle; //导入依赖的package包/类
public static <A extends CharacterStyle, B extends CharacterStyle> Spannable replaceAll(Spanned original,
Class<A> sourceType,
SpanConverter<A, B> converter,
final ClickSpan.OnClickListener listener) {
SpannableString result = new SpannableString(original);
A[] spans = result.getSpans(0, result.length(), sourceType);
for (A span : spans) {
int start = result.getSpanStart(span);
int end = result.getSpanEnd(span);
int flags = result.getSpanFlags(span);
result.removeSpan(span);
result.setSpan(converter.convert(span, listener), start, end, flags);
}
return (result);
}
示例6: findStartPosition
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* 检查当前位置是否命中在spannable上, 如果是, 返回spannable的start position
*/
private int findStartPosition(Spannable spannable, int startWidthPosition) {
CharacterStyle[] oldSpans = spannable.getSpans(startWidthPosition, spannable.length(), CharacterStyle.class);
int position = startWidthPosition;
for (CharacterStyle oldSpan : oldSpans) {
int spanStart = spannable.getSpanStart(oldSpan);
int spanEnd = spannable.getSpanEnd(oldSpan);
if (spanStart <= startWidthPosition && spanEnd > startWidthPosition) {
position = spanStart;
}
if (spanStart >= startWidthPosition) {
spannable.removeSpan(oldSpan);
}
}
//L.e("call: findStartPosition([spannable, startWidthPosition]) " + startWidthPosition + " -> " + position);
return position;
}
示例7: setSpanBetweenTokens
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* Given either a Spannable String or a regular String and a token, apply
* the given CharacterStyle to the span between the tokens.
* <p/>
* NOTE: This method was adapted from:
* http://www.androidengineer.com/2010/08/easy-method-for-formatting-android.html
* <p/>
* <p/>
* For example, {@code setSpanBetweenTokens("Hello ##world##!", "##", new
* ForegroundColorSpan(0xFFFF0000));} will return a CharSequence {@code
* "Hello world!"} with {@code world} in red.
*/
private CharSequence setSpanBetweenTokens(CharSequence text, String token,
CharacterStyle... cs) {
// Start and end refer to the points where the span will apply
int tokenLen = token.length();
int start = text.toString().indexOf(token) + tokenLen;
int end = text.toString().indexOf(token, start);
if (start > -1 && end > -1) {
// Copy the spannable string to a mutable spannable string
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
for (CharacterStyle c : cs)
ssb.setSpan(c, start, end, 0);
text = ssb;
}
return text;
}
示例8: withinParagraph
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* Convert a spanned text within a paragraph
*/
private void withinParagraph(final Spanned text, int start, int end) {
// create sorted set of CharacterStyles
SortedSet<CharacterStyle> sortedSpans = new TreeSet<>(new Comparator<CharacterStyle>() {
@Override
public int compare(CharacterStyle s1, CharacterStyle s2) {
int start1 = text.getSpanStart(s1);
int start2 = text.getSpanStart(s2);
if (start1 != start2)
return start1 - start2; // span which starts first comes first
int end1 = text.getSpanEnd(s1);
int end2 = text.getSpanEnd(s2);
if (end1 != end2) return end2 - end1; // longer span comes first
// if the paragraphs have the same span [start, end] we compare their name
// compare the name only because local + anonymous classes have no canonical name
return s1.getClass().getName().compareTo(s2.getClass().getName());
}
});
List<CharacterStyle> spanList = Arrays.asList(text.getSpans(start, end, CharacterStyle.class));
sortedSpans.addAll(spanList);
// process paragraphs/divs
convertText(text, start, end, sortedSpans);
}
示例9: handleEndTag
import android.text.style.CharacterStyle; //导入依赖的package包/类
private void handleEndTag(CharacterStyle style) {
if (style instanceof URLSpan) {
mOut.append("</a>");
} else if (style instanceof TypefaceSpan) {
mOut.append("</font>");
} else if (style instanceof ForegroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof BackgroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof AbsoluteSizeSpan) {
mOut.append("</font>");
} else if (style instanceof StrikethroughSpan) {
mOut.append("</strike>");
} else if (style instanceof SubscriptSpan) {
mOut.append("</sub>");
} else if (style instanceof SuperscriptSpan) {
mOut.append("</sup>");
} else if (style instanceof UnderlineSpan) {
mOut.append("</u>");
} else if (style instanceof BoldSpan) {
mOut.append("</b>");
} else if (style instanceof ItalicSpan) {
mOut.append("</i>");
}
}
示例10: init
import android.text.style.CharacterStyle; //导入依赖的package包/类
private void init(CharSequence source, int start, int end) {
int initial = 20;
mSpans = new Object[initial];
mSpanData = new int[initial * 3];
if (source instanceof Spanned) {
Spanned sp = (Spanned) source;
for (Object span : sp.getSpans(start, end, Object.class)) {
if (span instanceof CharacterStyle || span instanceof ParagraphStyle) {
int st = sp.getSpanStart(span);
int en = sp.getSpanEnd(span);
int fl = sp.getSpanFlags(span);
if (st < start) st = start;
if (en > end) en = end;
setSpan(span, st - start, en - start, fl);
}
}
}
}
示例11: withinParagraph
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* Convert a spanned text within a paragraph
*/
private void withinParagraph(final Spanned text, int start, int end) {
// create sorted set of CharacterStyles
SortedSet<CharacterStyle> sortedSpans = new TreeSet<CharacterStyle>(new Comparator<CharacterStyle>() {
@Override
public int compare(CharacterStyle s1, CharacterStyle s2) {
int start1 = text.getSpanStart(s1);
int start2 = text.getSpanStart(s2);
if (start1 != start2)
return start1 - start2; // span which starts first comes first
int end1 = text.getSpanEnd(s1);
int end2 = text.getSpanEnd(s2);
if (end1 != end2) return end2 - end1; // longer span comes first
// if the paragraphs have the same span [start, end] we compare their name
// compare the name only because local + anonymous classes have no canonical name
return s1.getClass().getName().compareTo(s2.getClass().getName());
}
});
List<CharacterStyle> spanList = Arrays.asList(text.getSpans(start, end, CharacterStyle.class));
sortedSpans.addAll(spanList);
// process paragraphs/divs
convertText(text, start, end, sortedSpans);
}
示例12: afterTextChanged
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* {@inheritDoc}
* @param s
*/
@Override
public void afterTextChanged(Editable s) {
for (CharacterStyle style: mLastStyle) {
s.removeSpan(style);
}
List<MarkdownSyntaxModel> models = MarkdownSyntaxGenerator.syntaxModelsForString(s.toString());
if (models.size() == 0) {
return;
}
mLastStyle.clear();
for (MarkdownSyntaxModel model : models) {
MarkdownSyntaxType type = model.getSyntaxType();
Range range = model.getRange();
// CharacterStyle style = MarkdownSyntaxGenerator.styleFromSyntaxType(type);
int low = range.getLower();
int upper = range.getUpper();
// mLastStyle.add(style);
// s.setSpan(style, low, upper, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
示例13: highlight
import android.text.style.CharacterStyle; //导入依赖的package包/类
public static SpannableStringBuilder highlight(String text, String target) {
SpannableStringBuilder spannable = new SpannableStringBuilder(text);
CharacterStyle span = null;
for (int i = 0; i < specials.length; i++) {
if (target.contains(specials[i])) {
target = target.replace(specials[i], "\\" + specials[i]);
}
}
Pattern p = Pattern.compile(target.toLowerCase());
Matcher m = p.matcher(text.toLowerCase());
while (m.find()) {
span = new ForegroundColorSpan(Color.rgb(253, 113, 34));// 需要重复!
spannable.setSpan(span, m.start(), m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannable;
}
示例14: replaceControlCodes
import android.text.style.CharacterStyle; //导入依赖的package包/类
/**
* Replace the control codes
*
* @param m
* @param ssb
* @param style
*/
private static void replaceControlCodes(Matcher m, SpannableStringBuilder ssb, CharacterStyle style) {
ArrayList<Integer> toremove = new ArrayList<Integer>();
while ( m.find() ) {
toremove.add(0, m.start());
// Remove the ending control character unless it's \x0F
if( m.group(2) != null && m.group(2) != m.group(3) ) {
toremove.add(0, m.end() - 1);
}
ssb.setSpan(style, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
for( Integer i : toremove ) {
ssb.delete(i, i + 1);
}
}
示例15: getSize
import android.text.style.CharacterStyle; //导入依赖的package包/类
@Override
public int getSize(@NonNull Rect outRect, @NonNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm) {
int width = super.getSize(outRect, paint, text, start, end, fm);
if (styles != null) {
for (CharacterStyle style : styles) {
if (style instanceof SupportSpan) {
width = Math.max(width, ((SupportSpan) style).getSize(frame, paint, text, start, end, fm));
} else if (style instanceof ReplacementSpan) {
width = Math.max(width, ((ReplacementSpan) style).getSize(paint, text, start, end, fm));
} else if (paint instanceof TextPaint) {
if (style instanceof MetricAffectingSpan) {
((MetricAffectingSpan) style).updateMeasureState((TextPaint) paint);
}
}
}
}
frame.right = width;
return width;
}