mar*_*rio 5 android spanned spannablestring
我有 3 个字符串本地化
<string name="tests" formatted="true">Test<annotation font="bold"> testBold %1$s</annotation> end</string>
<string name="tests" formatted="true">????<annotation font="bold"> ???????? %1$s</annotation> ?????</string>
<string name="tests" formatted="true">????<annotation font="bold"> ???????? %1$s</annotation> ??????</string>
Run Code Online (Sandbox Code Playgroud)
我如何通过注释添加一些参数和修改后的文本。我得到的最大的是做这件事
CharSequence t = getResources().getString(R.string.tests, "myValue");//in this case i lose my annotation, but set my argument
//OR
CharSequence t = getText(R.string.tests);//in this case i lose my argument but get style BOLD
public SpannableString textFormattingByTags(CharSequence t) {
SpannedString titleText = new SpannedString(t);
SpannedString titleText = (SpannedString) getText(R.string.tests);
Annotation[] annotations = titleText.getSpans(0, titleText.length(), Annotation.class);
SpannableString spannableString = new SpannableString(titleText);
for (Annotation annotation : annotations) {
if (annotation.getKey().equals("font")) {
String fontName = annotation.getValue();
if (fontName.equals("bold")) {
spannableString.setSpan(new CustomTypefaceSpan("",fontBold),
titleText.getSpanStart(annotation),
titleText.getSpanEnd(annotation),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return spannableString;
}
Run Code Online (Sandbox Code Playgroud)
结果在第一种情况下,我在第二个“Test testBold %1$s end”中得到“Test testBold MyValue end”。谁有一些想法?
1. 将参数转换为注释
你的字符串:
<string name="tests" formatted="true">Test<annotation font="bold"> testBold %1$s</annotation> end</string>
Run Code Online (Sandbox Code Playgroud)
变成:
<string name="tests">Test<annotation font="bold"> testBold <annotation arg="0">%1$s</annotation></annotation> end</string>
Run Code Online (Sandbox Code Playgroud)
2.从资源创建SpannableStringBuilder
val text = context.getText(R.string.tests) as SpannedString
val spannableText = SpannableStringBuilder(text)
Run Code Online (Sandbox Code Playgroud)
3. 首先应用所有 arg 注释
一个示例实现:
fun SpannableStringBuilder.applyArgAnnotations(vararg args: Any) {
val annotations = this.getSpans(0, this.length, Annotation::class.java)
annotations.forEach { annotation ->
when (annotation.key) {
"arg" -> {
val argIndex = Integer.parseInt(annotation.value)
when (val arg = args[argIndex]) {
is String -> {
this.replace(
this.getSpanStart(annotation),
this.getSpanEnd(annotation),
arg
)
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
传入参数:
spannableText.applyArgAnnotations("myValue")
Run Code Online (Sandbox Code Playgroud)
4. 应用剩余注释
spannableText.applyAnnotations()
textView.text = spannableText
Run Code Online (Sandbox Code Playgroud)
5. 结果
测试testBold myValue结束
创建了一个小库来抽象此行为 - https://github.com/veritas1/tinytextstyler
Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/Trebuchet_MS_Bold.ttf");
String s = getResources().getString(R.string.error_date, "20.02.2019", "25.02.2019");
SpannableStringBuilder spannableString = new SpannableStringBuilder(s);
Integer first1 = null;
Integer first2 = null;
Integer last1 = null;
Integer last2 = null;
int digits = 0;
char[] crs = s.toCharArray();
for (int i = 0; i < crs.length; i++) {
if (Character.isDigit(crs[i]) && digits != 8) {
if (first1 == null) {
first1 = i;
}
last1 = i;
digits++;
continue;
}
if (Character.isDigit(crs[i])) {
if (first2 == null) {
first2 = i;
}
last2 = i;
}
}
spannableString.setSpan(new CustomTypefaceSpan("", fontBold), first1, last1 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new CustomTypefaceSpan("", fontBold), first2, last2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getInstance());
builder.setTitle("test");
builder.setMessage(spannableString);
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getLoaderManager().destroyLoader(LOADER_SOE_BILLING_ID);
}
});
android.support.v7.app.AlertDialog alert = builder.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)