We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to apply styles to specific words within a string. For now I tried creating a paragraph that contains the styles inside (with the idea of appending each word with its style on a string):
var paragraph = "hello <span style=\"color:blue;\">blueeee</span> adios";
txt = createP(paragraph);
For some reason when this is rendered in html, it introduces quotation marks:
<p>
"hello "
"span style=\"color:blue;\">blueeee</span>"
" adios"
</p>
Another alternative I though about would be to append word by word (with they specific style), but I'm not sure how to avoid the line break.
Any suggestions? Thanks!
Answers
Hi GoToLoop, thanks for your answer. I'm still a bit confused though. From 1. I see title being formatted, but I'm not sure how this gets into a string or paragraph. From 2. I see how you can format strings, but not how you can combine these formatted strings into one (keeping diverse formatting) or combine into a paragraph.
Basically what I'm looking for is constructing a sentence like "Hello this is my house" where this and house are variables, and have different styles than the rest.
Thanks!
https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
It's much easier to help you out if you had a runnable online sample sketch.
You can host p5.js online here for example: https://OpenProcessing.org/sketch/create
I'm using createP here, but not sure is the right way to do it: https://openprocessing.org/sketch/443830
This way I'm not able to programatically change the color depending on the word, I believe? Like if (word == 'house') then make it blue, if (word == 'tree') make it green.
That's why I was trying two ways: 1. Adding parts with the formatting embedded (that's what I was trying to do with the span) 2. Create the whole string and then look for specific words, and style them.
What if you had pairs that could be looked up by the same loop index?:
...and THEN, instead COLOR being a const, what if you built the second list programmatically based on the values in the first list?
How can I have a loop with the createP? If I create multiple createP, these will be adding words in separate lines, right?
Here's what I'm trying to do, to be clear: from speech-to-text, I'll receive a string, something like:
"In the morning I've been in the park, I had chicken for lunch and then I went biking with my friends."
I'll have a JSON file with keywords, like: park, chicken and friends.
Then, after speech has been transribed to text, I want to highlight those keywords, each with a different color:
"In the morning I've been in the park, I had chicken for lunch and then I went biking with my friends."
https://OpenProcessing.org/sketch/443843
index.html:
sketch.js:
For example:
Thanks so much! That worked, and I managed to read the words and colors from a JSON file too. ` function colorizeWords2(txt){ const flags = 'gi', matched = '$&'; // g = global match, don't stop after the first one // i = ignore case
} `
One last questions - besides fontcolor, is it possible to either color the background, or the color of the underscore? Like this:
I couldn't find a method to do that here: https://www.w3schools.com/jsref/jsref_fontcolor.asp
But there may be another option?
Thanks so much again!
<font color=""></font>
tag.'Processing'.fontcolor('yellow')
in its console.'Processing'
becomes this string now:"<font color="yellow">Processing</font>"
<span>
+ attribute background-color: https://Developer.Mozilla.org/en-US/docs/Web/CSS/background-colorfor...in
loop within colorizeWords()./[-\w\xC0-\u02AF]+/g
.new
1s for each word of the passed object list.true
, it returns the matched word wrapped up w/ a styling tag for the found colour.index.html:
sketch.js:
class
Colorize asstatic
methods.static
properties within Colorize.Colorize.colorizeWords(msg, COLOR_WORDS, true)