SpannableStringBuilder setSpan不能处理阿拉伯文

时间:2015-06-17 07:08:42

标签: java android arabic spannablestring

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

enter image description here

这是我的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?

任何人都可以帮助我吗?

更新:

这些都不会使文字变成红色:

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库我用来加载我的自定义阿拉伯字体。当我删除它时,setSpan工作了。我已经开始使用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);

结果:

enter image description here

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

的形式

1 个答案:

答案 0 :(得分:1)

使用Calligraphy代替PixUI。以下是Gradle dependecy:

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的颜色代码,如0xAARRGGBB,@ pskink建议:

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