3

This may be a blunder,But I need to know... :) Iam develeoping an android application,In in I want to display two type face in a single textview and found this One very useful,A custom typeface span that extends typeface span, As per my understanding , we are creating a spannable as follows using two words

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

then sets two type face to that words using our custom span like this

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

Now my question, I have a large amount of text so If i am able to put the spans directly in my text its will be a more easy for us ,is that possible to put spans directly to the text like we put <b>bold </b> as my need is to use my custom typeface span like <typeface1> sample </typeface1> <typeface2> Doc </typeface2>.

WHAT I NEED IS, DIRECTLY APPLY THE CHANGE TO TEXT WHAT THE SET SPAN DO TO THE TEXT

Is that possible, if possible what i need to write as span in my text,How can I achieve this.. or am i compleatly wrong ?

Thank you for all

Renjith K N
  • 2,613
  • 2
  • 31
  • 53

1 Answers1

2

Very late answer but what you asked for is pretty easy.

For instance, lets say we want to change everything between quotation marks into italic font.

    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 );

Result of s would be

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

Now instead of quotation marks you would use a regex pattern for tags like <b>bold </b>;

And you could also remove the tags afterwards with a simple .replace();

Edward van Raak
  • 4,841
  • 3
  • 24
  • 38
  • Thank you Edvard for the answer, this is not the Thing I want,In answer we are using spannable.setSpan to set the span to desired index, I want to avoid setspan statement from my code...and directly apply the typeface to text itself without any further modification...Soryy may be it's a blunder :) – Renjith K N Aug 22 '13 at 03:57