如何避免 SpannableString 跨越数字
how to avoid SpannableString to span digits
我正在尝试使用 Spannable String 来跨越字符串而不跨越数字。
String s = "asd21da";
我想避免对数字进行任何更改,只是跨越字符。
可能吗?
我的代码:
@SuppressLint("ParcelCreator")
class TypeFace extends TypefaceSpan {
Typeface typeface;
public TypeFace(String family, Typeface typeface) {
super(family);
this.typeface = typeface;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setTypeface(typeface);
}
@Override
public void updateMeasureState(TextPaint ds) {
ds.setTypeface(typeface);
}
}
public SpannableString spannableString(String s) {
SpannableString span = new SpannableString(s);
span.setSpan(new TypeFace("", Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf")), 0, span.length(), span.SPAN_EXCLUSIVE_EXCLUSIVE);
return span;
}
我用它来更改字符串的字体,但我试图避免更改数字字体。
一种方法是将跨度设置为每个字符(如果它不是数字):
SpannableString span = new SpannableString(s);
for (int i = 0; i < span.length(); i++) {
if (Character.isDigit(span.charAt(i)))
continue;
span.setSpan(new TypeFace("", Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf")), i, i+1, span.SPAN_INCLUSIVE_INCLUSIVE);
}
我正在尝试使用 Spannable String 来跨越字符串而不跨越数字。
String s = "asd21da";
我想避免对数字进行任何更改,只是跨越字符。 可能吗?
我的代码:
@SuppressLint("ParcelCreator")
class TypeFace extends TypefaceSpan {
Typeface typeface;
public TypeFace(String family, Typeface typeface) {
super(family);
this.typeface = typeface;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setTypeface(typeface);
}
@Override
public void updateMeasureState(TextPaint ds) {
ds.setTypeface(typeface);
}
}
public SpannableString spannableString(String s) {
SpannableString span = new SpannableString(s);
span.setSpan(new TypeFace("", Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf")), 0, span.length(), span.SPAN_EXCLUSIVE_EXCLUSIVE);
return span;
}
我用它来更改字符串的字体,但我试图避免更改数字字体。
一种方法是将跨度设置为每个字符(如果它不是数字):
SpannableString span = new SpannableString(s);
for (int i = 0; i < span.length(); i++) {
if (Character.isDigit(span.charAt(i)))
continue;
span.setSpan(new TypeFace("", Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf")), i, i+1, span.SPAN_INCLUSIVE_INCLUSIVE);
}