テキスト周りのまとめ
size(400, 300); background(255); fill(0); textFont(createFont("FixedSys", 32)); int halfW = width>>1, halfH = height>>1; int top = (int)textAscent(); int bottom = (int)textDescent(); String str = "へろーわーるど"+str(top+bottom); int x = halfW - ((int)textWidth(str)>>1); int y = halfH+top; text(str, x, y); // 中心線とかぶるためずらず line(0,y-top+1,width,y-top+1); line(0,y+bottom,width,y+bottom); stroke(255, 0, 0); line(halfW, 0, halfW, height); line(0, halfH, width, halfH);
テキストの位置を調整するために覚えておいたほうがいい関数は3つ
textAscent
http://processing.org/reference/textAscent_.html
現在のフォントの種類、サイズで表示できる全ての文字の縦幅の中心から上の長さ
textDescent
http://processing.org/reference/textDescent_.html
↑の縦幅の中心から下半分の長さ。
つまりtextAscent()+textDescent()で現在のフォントに必要な分の縦幅の長さが分かる。
これだけ用意しておけば文字が縦方向にはみ出すことない。(はず!!)
textWidth
http://processing.org/reference/textWidth_.html
現在のフォントサイズで、引数の文字列を表示したときの長さが返ってくる。