Wrap the text and Change the specific word colour in a text file

the first thing I want to do is wrap the text and the second thing is to make the color change of specific words, but I found it is hard to do. Please somebody can help me!!!

void setup() {
size(600, 600);
background(0);
String []l = loadStrings(“l.csv”);
println(“there are " + l.length + " lines”);
String lot = join(l, " ");
String [] words = splitTokens( lot, ", ");

translate(20, 20);
float x =10;

for (int i = 0; i < words.length; i++) {
textSize(10);
char c = lot.charAt(i);
x = x+textWidth©;
fill(random(255));
text(c,x, 0);
}

}

void draw() {
}

1 Like

hi,
first you should try to copy your pasted code back to PDE
and see that it is not running.
that’s because you not pasted it using the

</> Preformatetd text

button from the forum editor menu, looks like

```
type or paste code here
```

also as we not have that file you load
we like to see a MCVE a code what runs without files need unless you provide them as external .zip


your code looks like you want a different color for each character not each word??

this would be a version what use colors words but still prints characters
( uselessly but just to show what heavy problem you have, if you try to do that character positioning manually )

String[] l = { "test1, test2, test3", "test11, test12, test13" };
String[] words;
String lot;

void setup() {
  size(600, 600);
  background(0);
  //l = loadStrings("l.csv");  // not available
  println("there are " + l.length + " lines");
  print("l: ");
  println(l);
  lot = join(l, " ");
  print("lot: ");
  println(lot);
  words = splitTokens( lot, ", ");
  print("words: ");
  println(words);
  noLoop();
}

void draw() {
  background(0, 0, 80);
  textSize(20);
  fill(0);
  float x =10;
  for (int i = 0; i < words.length; i++) {
    fill(random(50, 255), random(50, 255), 0);
    for ( int j = 0; j < words[i].length(); j++ ) {
      char c = words[i].charAt(j);
      x = x + textWidth(c);
      text(c, x, 20);
    }
    x+=10; // between the words
  }
}

3 Likes

Because of the way text works in Processing, formatting individual words is hard in the general case.

Here is an example of formatting words in a text box: SentenceColoredText. It uses a “bricklaying method” to fill the space with text units.

https://forum.processing.org/two/discussion/comment/87679/#Comment_87679

Also, a small markup language for text formatting from @Chrisir :

https://forum.processing.org/two/discussion/comment/120813/#Comment_120813

2 Likes