3

これは失敗かもしれませんが、知っておく必要があります... :) Androidアプリケーションを開発しています.2つのタイプフェイスを1つのテキストビューに表示したいのですが、これは非常に便利であることがわかりました.タイプフェイススパンを拡張するカスタムタイプフェイススパン、私の理解によると、2つの単語を使用して次のようにスパン可能を作成しています

String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);

次に、このようなカスタム スパンを使用して、2 つの書体をその単語に設定します

// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,              
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE),   rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

今私の質問です、私は大量のテキストを持っているので、テキストに直接スパンを入れることができれば、それは私たちにとってより簡単になります.私の必要に応じてテキストにスパンを直接入れることが可能<b>bold </b>ですか?のようなカスタム書体スパンを使用するには<typeface1> sample </typeface1> <typeface2> Doc </typeface2>

私が必要としているのは、テキストに変更を直接適用するセットスパンがテキストに行うことです

それは可能ですか、可能であればテキストにスパンとして書く必要があるもの、どうすればこれを達成できますか..または私は完全に間違っていますか?

すべてをありがとう

4

1 に答える 1

2

非常に遅い回答ですが、あなたが求めたものはかなり簡単です。

たとえば、引用符の間のすべてを斜体フォントに変更したいとします。

    String s =  "\"Hello my name is Edward\" Hola, my nombre es Edward";        
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(text);
    Spannable spannable = new SpannableString(s);
    while (m.find()) {        
      int textLocation = text.indexOf(m.group(1)); 

      // Set the custom typeface to span over a section of the spannable object       
       spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
       textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                   

      // Set the text of a textView with the spannable object
      TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);

    }
    textbox.setText( spannable );

の結果s

"Hello my name is Edward" Hola, my nombre es Edward

引用符の代わりに、次のようなタグに正規表現パターンを使用します<b>bold </b>

.replace()また、後で簡単な;を使用してタグを削除することもできます。

于 2013-08-21T18:42:04.933 に答える