任何将span直接设置为spanable文本的方法



这可能是个错误,但我需要知道…:)我正在开发一个android应用程序,在中,我想在一个文本视图中显示两个字体,并发现这个非常有用,一个扩展字体跨度的自定义字体跨度,根据我的理解,我们正在使用两个单词创建一个如下的spanable

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

然后使用我们的自定义跨度(如)将两种类型的人脸设置为该单词

// 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>

我需要的是,直接将更改应用于文本设置跨度对文本的作用

这可能吗,如果可能的话,我需要在我的文本中写些什么,我如何才能做到这一点。。还是我完全错了?

感谢所有

答案很晚,但您的要求很简单。

例如,假设我们想将引号之间的所有内容都更改为斜体。

    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的结果将是

"你好,我叫Edward"Hola,我的名字叫Edward

现在,对于像<b>bold </b>这样的标签,您将使用正则表达式模式,而不是引号;

然后你也可以用一个简单的.replace()删除标签;

最新更新