NoName
February 27, 2020, 10:51am
1
Hello.
Just a few questions.
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.
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
Chrisir
February 27, 2020, 10:55am
2
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
Chrisir
February 27, 2020, 11:57am
4
Waboqueox:
fill(245,0,0); text(“Hello”, 14, 14); fill(0,255,0); text(“\nNew line”, 14, 14);
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
NoName
February 27, 2020, 9:51pm
5
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