How to iterate through a string and assign differe

2019-05-22 16:55发布

问题:

I want to change the font color of certain keywords. What I'm trying to achieve is similar to the text editors of programming environments where certain keywords are displayed in different colors.

For example "printf" in red,"scanf" in green,brackets in dark blue etc.

Please note that I'll be receiving the string as an extra from intent. i.e,It is not a fixed sentence......the string could contain any number of words in any combination.

All I want to do is change the font color of certain words

回答1:

Iterating through a Spannable to find words to color is usually a matter of indexOf() or similar methods on TextUtils.

Coloring words is a matter of applying a ForegroundColorSpan.

This sample project demonstrates this, albeit with a BackgroundColorSpan. The key method is:

  private void searchFor(String text) {
    TextView prose=(TextView)findViewById(R.id.prose);
    Spannable raw=new SpannableString(prose.getText());
    BackgroundColorSpan[] spans=raw.getSpans(0,
                                             raw.length(),
                                             BackgroundColorSpan.class);

    for (BackgroundColorSpan span : spans) {
      raw.removeSpan(span);
    }

    int index=TextUtils.indexOf(raw, text);

    while (index >= 0) {
      raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
          + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      index=TextUtils.indexOf(raw, text, index + text.length());
    }

    prose.setText(raw);
  }

Here, given the existence of a TextView that contains the desired text to colorize, and given a string to search for (text), we remove all existing BackgroundColorSpans, then find all occurrences of the search term and apply new BackgroundColorSpans.



回答2:

I got the answer I was looking for:

public class MainActivity extends Activity {
TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        String x="Hello My Name is Umang Mathur";
        tv.setText(x);
        String [] keywordspurple={"Umang","Mathur","Hello"};
        for(String y:keywordspurple)
        {
        fontcolor(y,0xFF8B008B);
        }
        String [] keywordsgreen={"Name","is"};
        for(String y:keywordsgreen)
        {
        fontcolor(y,0xffff0000);
        }
    }

 private void fontcolor(String text,int color) {
            TextView prose=(TextView)findViewById(R.id.textView1);
            Spannable raw=new SpannableString(prose.getText());
            int index=TextUtils.indexOf(raw, text);
            while (index >= 0) {
              raw.setSpan(new ForegroundColorSpan(color), index, index
                  + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
              index=TextUtils.indexOf(raw, text, index + text.length());
            }
            prose.setText(raw);
          }

This code resulted in setting 3 words in puple,2 in green and the word "My" was left as it is in black.