Multiple Styles or Colors in PIXI.Text object

I've been all over the PIXI Documentation, and have googled around for a few hours looking for an answer to this. I'm making a custom Text Manager for the project I'm working on. I can get it to do everything I want so far, but I'm having trouble with this one thing.

I want to be able to change the color of individual words in a PIXI.Text object.

From what I've researched, it doesn't seem possible to do it with one PIXI.Text object. When I change anything about the style(PIXI.TextStyle object), it affects all of the text.

So lets say I have the sentence
"I am going to go watch old people use stairs"
and I wanted to affect the style of just the words 'old people'

is there any way to do this without having to go through the headache of using and positioning multiple PIXI.Text objects? I'm using wordwrap as part of the style, and I'm even using a PIXI.TextMetrics object to measure the text ahead of time, and reading the .lines property of it to avoid printing a wrapped word on it's initial line, and just wrapping it instantly.

So it would be great if I didn't have to use a bunch of extra Text objects and position them.

Any ideas?
So I decided to try a different approach.
I hate doing a bunch of positioning. I'm really, really bad at math, and believe it or not a computer can only help you so much. You still need to know higher mathematical concepts to really get good at graphics stuff in coding.

But anyway, I realized that the last time I did something like break all text into individual words, the positioning was a nightmare, but that was before I was messing with PIXI Text Metric.

Anyway, I fixed my problem, and the text metric actually helps a lot with the positioning, even with my bad math skills XD. Here's some of the code that I wrote.

This example assumes you already have a PIXI.TextStyle object as a property belonging to this, and that the style object has
- .wordWrap == true
- .wordWrapWidth == Graphics.width * .9

It also assumes you already have your text measured in a TextMetric object, using the above style


spDialogue.prototype.makeSentenceContainers = function(){
let met = this.metric;
this.allText = new PIXI.Container();
for(let i in met.lines){
this.allText.addChild(new PIXI.Container());
}
}

spDialogue.prototype.breakSentences = function(){
let met = this.metric;
let spaceMet = new PIXI.TextMetrics.measureText(" ", this.style);
let lastMet = {};
let line = "";
let i = "";
let j = "";
let cnt = 0;
for(i in met.lines){
line = met.lines.split(" ");
cnt = 0;
this.allText.children.y = met.lineHeight * i;
for(j of line){
this.allText.children.addChild(new PIXI.Text(j, this.style))
if(cnt > 0){
lastMet = new PIXI.TextMetrics.measureText(this.allText.children.children[cnt - 1].text, this.style)
this.allText.children.children[cnt].x =
this.allText.children.children[cnt - 1].x + lastMet.width;
this.allText.children.children[cnt].x += spaceMet.width;
}
cnt++
}
}

}




Now from here, I can use the markup language I've designated to indicate which of these words to apply a different style to. Since they're all Text objects, they can have their own style.

Once I'm finished displaying it (my Text Manager of course displays by the letter, as most do) , I can render the entire display container as a texture, and dispose of the Text Objects and containers.


Anyway, since there seems like there is no way without using a different library or creating my own to apply multiple styles to a text object, and I found my own solution, this thread can be closed.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/multiple-styles-or-colors-in-pixi-text-object.116826/