SpannableStringBuilder setSpan 不适用于阿拉伯文本

SpannableStringBuilder setSpan doesn't work on Arabic text

我正在尝试构建一个阿拉伯语字符串,其中包含不同颜色的空格字符,文本应为黑色,特殊字符应为红色,并且句子之间的大小不同。像这样:

这是我的 TexView 定义:

<com.neopixl.pixlui.components.textview.TextView
    android:id="@+id/sura"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/kuran_sure_text_size"
    android:textDirection="rtl"
    pixlui:typeface="fatih_araz_font.ttf"/>

这是我的代码:

SpannableStringBuilder ssb = new SpannableStringBuilder();

for (int i = 0;  i < contentJsonArray.length(); ++i) {
    JSONObject suraJson = (JSONObject)contentJsonArray.get(i);
    Log.d(LOG_TAG, "QuranPageFragment - WebServiceClient.getVerse - success - " +
            "Content " + i + " = " + suraJson.toString());
    ssb.append(suraJson.getString(KEY_VERSE));
    int start = ssb.length();

    if (i == contentJsonArray.length() - 1)
        break;

    // This has to be red and small but it doesn't
    ssb.append(" (#) ");
    ssb.setSpan(new ForegroundColorSpan(R.color.Red), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new AbsoluteSizeSpan(10, true), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Log.d(LOG_TAG, "QuranPageFragment - WebServiceClient.getVerse - success - Sura = " + ssb.toString());
suraTextView.setText(ssb, TextView.BufferType.SPANNABLE);

我已经尝试了以下问题的解决方案,但仍然无法使 " (#) " 变红变小:

SpannableStringBuilder to create String with multiple fonts/text sizes etc Example?

谁能帮帮我?

更新:

None 个使文本变红:

ssb.setSpan(new ForegroundColorSpan(0xFF0000), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new ForegroundColorSpan(0xFF0000), start, ssb.length(), 0);
ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.Red)), start, ssb.length(), 0);

这也不起作用:

SpannableStringBuilder ssb = new SpannableStringBuilder();

for (int i = 0;  i < contentJsonArray.length(); ++i) {
    JSONObject suraJson = (JSONObject)contentJsonArray.get(i);
    Log.d(LOG_TAG, "QuranPageFragment - WebServiceClient.getVerse - success - " +
            "Content " + i + " = " + suraJson.toString());
    Spannable spannable = new SpannableString(suraJson.getString(KEY_VERSE) + " (#) ");

    if (i == contentJsonArray.length() - 1)
        break;

    spannable.setSpan(
            new ForegroundColorSpan(
                    getResources().getColor(R.color.Red)),
            spannable.length() - 5,
            spannable.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(
            new AbsoluteSizeSpan(10, true),
            spannable.length() - 5,
            spannable.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.append(spannable);
}

Log.d(LOG_TAG, "QuranPageFragment - WebServiceClient.getVerse - success - Sura = " + ssb.toString());
suraTextView.setText(ssb);

已解决:

我已经解决了。问题是 PixUI library wich I used to load my custom Arabic font. When I removed it setSpan worked. I have swiched to Calligraphy 库加载我的自定义字体。

以下代码和布局工作正常:

<TextView
    android:id="@+id/sura"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/kuran_sure_text_size"
    android:textDirection="rtl"/>

SpannableStringBuilder ssb = new SpannableStringBuilder();

for (int i = 0;  i < contentJsonArray.length(); ++i) {
    JSONObject suraJson = (JSONObject)contentJsonArray.get(i);
    Log.d(LOG_TAG, "QuranPageFragment - WebServiceClient.getVerse - success - " +
            "Content " + i + " = " + suraJson.toString());
    ssb.append(suraJson.getString(KEY_VERSE));

    if (i == contentJsonArray.length() - 1)
        break;

    int start = ssb.length();
    ssb.append(" (#) ");
    ssb.setSpan(new ForegroundColorSpan(R.color.Red), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new AbsoluteSizeSpan(10, true), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Log.d(LOG_TAG, "QuranPageFragment - WebServiceClient.getVerse - success - Sura = " + ssb.toString());
CalligraphyTypefaceSpan typefaceSpan =
        new CalligraphyTypefaceSpan(
                TypefaceUtils.load(getActivity().getAssets(),
                        "fonts/fatih_araz_font.ttf"));
ssb.setSpan(typefaceSpan, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
suraTextView.setText(ssb);

结果:

@pskink 你对颜色代码的看法是正确的。它应该是 0xAARRGGBB

的形式

使用 Calligraphy 而不是 PixUI。这是它的 Gradle 依赖关系:

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

使用 TextView 而不是某些自定义 TextView,例如 PixUI TextView

<TextView
    android:id="@+id/sura"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/kuran_sure_text_size"
    android:textDirection="rtl"/>

构建结果文本后应用自定义字体:

SpannableStringBuilder ssb = new SpannableStringBuilder();

for (int i = 0;  i < contentJsonArray.length(); ++i) {
    JSONObject json = (JSONObject)contentJsonArray.get(i);
    ssb.append(json.getString(KEY_SOME_KEY));

    if (i == contentJsonArray.length() - 1)
        break;

    int start = ssb.length();
    ssb.append("( # )");
    ssb.setSpan(new ForegroundColorSpan(R.color.Red), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new AbsoluteSizeSpan(10, true), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

CalligraphyTypefaceSpan typefaceSpan =
        new CalligraphyTypefaceSpan(
                TypefaceUtils.load(getActivity().getAssets(),
                        "fonts/some_custom_font.ttf"));
ssb.setSpan(typefaceSpan, 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ssb);

使用 ForegroundColorSpan 的颜色代码,如 @pskink 建议的 0xAARRGGBB

ssb.setSpan(new ForegroundColorSpan(0xFFFF0000), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);