Text(); questions. Color and other

Hello.
Just a few questions.

  1. I know that you can paste text to a new line in text ();
    But I could not figure out how to do this?
    Can a simple example, I’ll figure it out.

  2. The question is also about text ();
    Is it possible to make the text color change right in the middle? Or not?

Thanks in advance.
The forum helps me a lot. You are best.

1 Like
fill(245,0,0); 
text("Hello   \nNew line", 14, 14);
2 Likes
fill(245,0,0); 
text("Hello", 14, 14);
fill(0,255,0); 
text("\nNew line", 14, 14);
3 Likes

Or in one line:

size(333, 333); 

background(0);

fill(245, 0, 0); 
text("Hello", 14, 14);
float xlen=textWidth("Hello"); 
fill(0, 255, 0); 
text("New text color", 14+xlen, 14);
3 Likes

Thanks a lot!)

By the way, Chrisir has been helping (saving) me many times already. You are the best.

1 Like

You cannot pass a single string to text() with multiple fills. However, you can call
text() multiple times. To align segments on a single line, use textWidth() to determine the offset.

String s1 = "Hello";
String s2 = " colorful";
String s3 = " World";
size(200, 100);
float offset = 10;
fill(0, 192, 0);
text(s1, offset, height/2);
offset += textWidth(s1);
fill(255, 0, 0);
text(s2, offset, height/2);
offset += textWidth(s2);
fill(0, 0, 255);
text(s3, offset, height/2);

Here it is with an array of words and colors, plus a loop:

String[] ss = new String[]{"Hello", " colorful", " World"};
color[] cc = new color[]{color(0, 192, 0), color(255, 0, 0), color(0, 0, 255)};
size(200, 100);
pushMatrix();
translate(10, height/2.0);
for(int i=0; i<ss.length; i++) {
  fill(cc[i]);
  text(ss[i],0,0);
  translate(textWidth(ss[i]), 0);
}
popMatrix();
3 Likes