Is it possible to display multicolor text with one call to Canvas.drawText ()?

I would like to use Canvas.drawText () to display multicolor text. In particular, I want to select a substring of text passed to the drawText () method.

The text is in the form of a SpannableString with 0 or more ForegroundColorSpan objects.

Looking at the Canvas code, it seems that calling .toString () for the passed CharSequence means that this is not possible.

Is there an alternative way?

EDIT: The text may change from time to time (total changes, not incremental). In addition, there are potentially several texts in a user view located in different, unrelated places.

+7
source share
4 answers

Yes, it is possible using one of the Layout classes. These are helper classes for drawing text on canvas, and they support Spannables. If your text does not change, use StaticLayout.

Example

Add this to your own view class.

private StaticLayout layout; 

put this code in onLayout or onSizeChanged

 Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers"); wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TextPaint paint = new TextPaint(); paint.setTextSize(20f); paint.setColor(Color.RED); layout = new StaticLayout(wordtoSpan, paint, getWidth(), Alignment.ALIGN_NORMAL, 1, 0, false); 

Then in your drawing method just call

 layout.draw(canvas); 

If your text changes frequently, you can use DynamicLayout .

 Editable.Factory fac = Editable.Factory.getInstance(); Editable edit = fac.newEditable(wordtoSpan); DynamicLayout layout = new DynamicLayout(edit,paint,getWidth(),Alignment.ALIGN_CENTER,1,0,false); 

edit text using an editing object

 edit.append("hello"); 
+25
source

i hvn't used with Canvas. see below code as i used it in text mode.

 public TextView getTextClipArt1(){ TextView textView = new TextView(context); Typeface tf = new MyTypeface(context, 0).getTypeface(); Shader textShader=new LinearGradient(0, 0, 0, 30, new int[]{Color.GREEN,Color.BLUE}, new float[]{0, 1}, TileMode.CLAMP); textView.setTypeface(tf); textView.getPaint().setShader(textShader); textView.getPaint().setStyle(Paint.Style.STROKE); textView.getPaint().setStrokeWidth(2); textView.setText("ABC"); textView.setTextSize(30); textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return textView; } 

you can now draw a textview as a bitmap on a canvas, although I think these methods also exist in the paint class. I hope you find it helpful.

+1
source

Try something like this if you use TextView

 String multiColorText = "<font color=0xff0000>Multi</font><font color=0x000000>Color</font><font color=0xccffff>Text</font>"; textView.setText(Html.fromHtml(multiColorText)); 

Edit: For SpannableString check if below supports

 Spannable WordtoSpan = new SpannableString("partial colored text"); WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
0
source

Whenever you write this text for this view, you can set thisView.setBackgroundResource (R.drawable.multicolor); and

In multicolor.xml write

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@color/tabBgStart" android:endColor="@color/tabBgEnd" android:angle="270"/> </shape> 

Hope it will work definitely.

To change the color of the text, you can use yourView.setTextColor (R.drawable.multicolor);

0
source

All Articles