Processing 如何在处理过程中获取文本字段的高度?

Processing 如何在处理过程中获取文本字段的高度?,processing,Processing,我正在显示一些不同字体和颜色的文本字段,它们彼此下方。 问题是,似乎没有办法计算每个字段的高度,因此很难将它们整齐地排成一行 我的代码: fill(245); textFont(alfaslab,48); text("Title text here, can be very short or very long", 20, 60, 580, 180); textFont(asapbold,30); text("More text here, can also be very short or v

我正在显示一些不同字体和颜色的文本字段,它们彼此下方。 问题是,似乎没有办法计算每个字段的高度,因此很难将它们整齐地排成一行

我的代码:

fill(245);
textFont(alfaslab,48);
text("Title text here, can be very short or very long", 20, 60, 580, 180);
textFont(asapbold,30);
text("More text here, can also be very short or very long", 20, 160, 480, 280);
fill(230);
textFont(asapreg,23);
text("More text here, can also be very short or very long", 20, 460, 480, 280);
这对你有帮助吗

pfontf;
无效设置(){
大小(550200);
int fontSize=40;//在这里进行更改,然后进行平均操作。。。
f=createFont(“arial”,字体大小);
textFont(f);
String hi=“你好,一切都好吗?”;
//武断的
浮点数xPos=20;
浮动yPos=高度/2;
文本(hi、XPO、YPO);
//宽度
浮动文本宽度=文本宽度(高);
//下y位置,包括文字下降
浮动下降=yPos+文本下降();
//从lowewr点计算的上限
浮动上限=下限-字体大小;
//你自己的盒子:)
中风(150);
行(xPos,更低,xPos+textWid,更低);
行(xPos,上部,xPos+textWid,上部);
线(xPos,上部,xPos,下部);
行(xPos+textWid,上部,xPos+textWid,下部);
//不考虑血统
冲程(255、220、220、180);
行(xPos、YPO、xPos+textWid、YPO);
}

此代码根据指定的宽度将长文本拆分为一行数组。 然后,您可以一行一行地显示每一行,以便始终知道使用了多少垂直空间。我在Stackoverflow的某个地方找到了这个答案。找不到原来的帖子了

//
// this list contains strings that fit within maxWidth
//
StringList wordWrap(String s, int maxWidth) {
  // Make an empty ArrayList
  StringList a = new StringList();
  float w = 0;    // Accumulate width of chars
  int i = 0;      // Count through chars
  int rememberSpace = 0; // Remember where the last space was
  // As long as we are not at the end of the String
  while (i < s.length()) {
    // Current char
    char c = s.charAt(i);
    w += textWidth(c); // accumulate width
    if (c == ' ') rememberSpace = i; // Are we a blank space?
    if (w > maxWidth) {  // Have we reached the end of a line?
      String sub = s.substring(0,rememberSpace); // Make a substring
      // Chop off space at beginning
      if (sub.length() > 0 && sub.charAt(0) == ' ') sub = sub.substring(1,sub.length());
      // Add substring to the list
      a.append(sub);
      // Reset everything
      s = s.substring(rememberSpace,s.length());
      i = 0;
      w = 0;
    } 
    else {
      i++;  // Keep going!
    }
  }

  // Take care of the last remaining line
  if (s.length() > 0 && s.charAt(0) == ' ') s = s.substring(1,s.length());
  a.append(s);

  return a;
}
现在我们调用将文本拆分为数组的函数,并逐个绘制每一行

  //
  // display each line of text one by one
  //
  void displayWrapped(String str, int fontSize) {
   StringList myTexts = wordWrap(str, 420);
   // loop through lines
    for (int i=0; i < myTexts.size(); i++) {
      String s = myTexts.get(i);
      text(s, 420, texty);
      texty += fontSize;
    }
  }
//
//逐个显示每行文本
//
void displayWrapped(字符串str,int-fontSize){
StringList MyText=wordWrap(str,420);
//环线
对于(int i=0;i
谢谢,但这只适用于一行。问题是我不知道这篇文章会有多长,也许它会被包装起来。我通过如下使用宽度和高度解决了这个问题:文本(“hello”,x,y,width,height)。这会自动包装文本,但您不知道有多少空间实际上被文本填充。@PietBinnenbocht您找到了多行高度问题的解决方案吗?我没有追求它:)但是您可以使用自己的函数来测量需要多少空间。。。例如:设置字体和大小,使用textWidth测量,根据需要拆分,计算行数,乘以字体垂直大小,以及。。。完成:)甚至创建您自己的文本框。是的,我使用发布为答案的函数解决了这个问题。它将文本拆分为一行数组。然后,您可以手动打印每一行,以便始终知道使用了多少垂直空间。
  //
  // display each line of text one by one
  //
  void displayWrapped(String str, int fontSize) {
   StringList myTexts = wordWrap(str, 420);
   // loop through lines
    for (int i=0; i < myTexts.size(); i++) {
      String s = myTexts.get(i);
      text(s, 420, texty);
      texty += fontSize;
    }
  }