0

I am having a hard time understanding how to calculate the size of a word in processing when there are different font sizes.

All I am trying to do is to put suffix right after the word "Metric". enter image description here

Does anyone know how to accomplish this?

//code

PFont f;
PFont f2;
void setup(){
  size(600, 300);

  f = createFont("SegoeUI-Semibold-200", 100);
  f2 = createFont("SegoeUI-Semibold-200", 20);

}

String metric = "Metric";

void draw(){ 

float sw = textWidth(metric); //how can I use sw?
fill(240);
textFont(f);
text(metric, 0, height-20);

fill(0);
textFont(f2);
text("sufix",300, height-20); //How can I calculate x to be at then of word Metric

}
4

1 回答 1

1

textWidth() 将考虑最后一次调用 font() 或 fontSize() 来计算宽度,因此您最好在正确调用 fontSize 或 font(font, size) 或 font() 后使用它。在您的情况下,它正在考虑声明的最后一个字体,即小字体...另外,我在绘图中调用了 background(),因此单词不会不断地被自己写出来,这会阻止它看起来很糟糕。

PFont f;
PFont f2;
void setup(){
  size(600, 300);

  f = createFont("SegoeUI-Semibold-200", 100);
  f2 = createFont("SegoeUI-Semibold-200", 20);

}

String metric = "anystuff";

void draw(){ 
  background(110);


fill(240);
textFont(f);
text(metric, 0, height-20);
float sw = textWidth(metric); //after font is set...

fill(0);
textFont(f2);
text("sufix",sw, height-20); //just use it :)

}
于 2013-05-03T12:49:49.507 回答