kart吧 关注:3贴子:118
  • 1回复贴,共1

text position information caculation

只看楼主收藏回复

1楼2014-10-11 22:37回复
    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, 0, 0, 180);
    line(xPos, yPos, xPos + textWid, yPos);
    }


    2楼2014-10-11 22:38
    回复