如何获取Processing中文本字段的高度?

时间:2014-02-28 17:15:37

标签: 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 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);

2 个答案:

答案 0 :(得分:1)

在这里,这对你有帮助吗?

PFont f;

void setup() {
  size (550, 200);

  int fontSize = 40;// change here and averything goes along...


  f = createFont("arial", fontSize);
  textFont(f);
  String hi = "Hi, there. How's every thing?";

  //arbitrary
  float xPos = 20;
  float yPos = height/2;

  text(hi, xPos, yPos);

  // width
  float textWid = textWidth(hi);

  //lower y position including descent of text
  float lower = yPos + textDescent();

  //upper calculated from lowewr point
  float upper = lower - fontSize;

  // your own box :)
  stroke(150);
  line(xPos, lower, xPos + textWid, lower);
  line(xPos, upper, xPos + textWid, upper );
  line(xPos, upper, xPos, lower );
  line(xPos + textWid, upper, xPos + textWid, lower );

  // not considering descent
  stroke(255, 220, 220, 180);
  line(xPos, yPos, xPos + textWid, yPos);
}

答案 1 :(得分:0)

此代码根据您指定的宽度将长文本分成单行数组。 然后,您可以逐个显示每一行,这样您就可以始终知道使用了多少垂直空间。我在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;
}

用法示例:定义全局变量texty。这将跟踪新文本行的当前y位置。每次添加新的文本行时都会更新。

function startDrawing() {
    texty=30;
    textFont(asapreg,23);
    displayWrapped("long text here", 23);

    texty+=30;
    textFont(asapreg,20);
    displayWrapped("another long text here", 20);
  }

现在我们调用将文本分成数组的函数,并逐个绘制每一行。

  //
  // 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;
    }
  }