font2
  • font height = descent + ascent
  • font size = font height - leading = descent + ascent - leading

android function

  • Paint.ascent()
    Return the distance above (negative) the baseline (ascent) based on the current typeface and text size.
  • Paint.descent()
    Return the distance below (positive) the baseline (descent) based on the current typeface and text size.
  • Paint.getFontMetrics().ascent
    The recommended distance above the baseline for singled spaced text.
  • Paint.getFontMetrics().descent
    The recommended distance below the baseline for singled spaced text.
  • Paint.getFontMetrics().leading
    The recommended additional space to add between lines of text.

Paint.setTextSize(72)

  • Paint.ascent() = -66.83203
  • Paint.descent() = 16.980469
  • paint.getFontSpacing() = 83.8125
  • Paint.getFontMetrics().ascent = -66.83203
  • Paint.getFontMetrics().descent = 16.980469
  • Paint.getFontMetrics().leading = 0.0
  • Paint.getFontMetrics().top = -75.44531
  • Paint.getFontMetrics().bottom = 19.511719

結論

  • 實際顯示 ÀÁÂABCfghijpqy 來說
  • canvas.drawText(text, x, y, paint);
  • if ((paint.descent() - paint.ascent()) > rect.height()) break;
    藉此來計算字型大小不會超出框框。
  • y = -paint.ascent();
    如此可以從 left-top 點畫出 ÀÁÂ。
  • font height = paint.descent() - paint.ascent() = paint.getFontSpacing()

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()