title: How to align text vertically?
body: <p>Target: Android >= 1.6 on a pure Canvas.</p>
  
  <p>Suppose I want to write a function that will draw a (width, height) large red rectangle and then draw a black <em>Hello World</em> text inside. I want the text to be visually in the center of the rectangle. So let's try:</p>
  
  <pre><code>void drawHelloRectangle(Canvas c, int topLeftX, 
          int topLeftY, int width, int height) {
      Paint mPaint = new Paint();
      // height of 'Hello World'; height*0.7 looks good
      int fontHeight = (int)(height*0.7);
  
      mPaint.setColor(COLOR_RED);
      mPaint.setStyle(Style.FILL);
      c.drawRect( topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint);
  
      mPaint.setTextSize(fontHeight);
      mPaint.setColor(COLOR_BLACK);
      mPaint.setTextAlign(Align.CENTER);
      c.drawText( "Hello World", topLeftX+width/2, ????, mPaint);
  }
  </code></pre>
  
  <p>Now I don't know what to put in drawText's argument marked by <code>????</code>, i.e. I don't know how to vertically align the text. </p>
  
  <p>Something like</p>
  
  <blockquote>
    <p>???? = topLeftY + height/2 +
    fontHeight/2 - fontHeight/8;</p>
  </blockquote>
  
  <p>appears to work more or less ok, but there must be a better way.</p>