如何垂直对齐文本? [英] How to align text vertically?

查看:129
本文介绍了如何垂直对齐文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:安卓> = 1.6上一个纯粹的画布

假设我想编写一个函数,将以此为(宽,高),大红色的矩形,然后画一个黑色的的Hello World 的文本中。我希望文本在视觉上的矩形的中心。所以让我们试试:

 无效drawHelloRectangle(帆布C,INT topLeftX,
        INT topLeftY,诠释的宽度,高度INT){
    油漆mPaint =新的油漆();
    //你好世界的高度;高* 0.7看起来不错
    INT fontHeight =(INT)(高* 0.7);

    mPaint.setColor(COLOR_RED);
    mPaint.setStyle(Style.FILL);
    c.drawRect(topLeftX,topLeftY,topLeftX +宽,topLeftY +高度,mPaint);

    mPaint.setTextSize(fontHeight);
    mPaint.setColor(COLOR_BLACK);
    mPaint.setTextAlign(Align.CENTER);
    c.drawText(Hello World的,topLeftX +宽/ 2,????,mPaint);
}
 

现在我不知道该放在标记的drawText的说法????什么,也就是我不知道该怎么垂直对齐文本。

类似于

  

???? = topLeftY +高/ 2 +   fontHeight / 2 - fontHeight / 8;

似乎工作或多或少确定,但必须有一个更好的办法。

解决方案

示例对 CX 中央和 CY

 私人最终矩形textBounds =新的矩形(); //没有新本在平局的方法

公共无效drawTextCentred(画布油画,漆漆,字符串文本,浮动CX,浮CY){
  paint.getTextBounds(文字,0,text.length(),textBounds);
  canvas.drawText(文字,CX  -  textBounds.exactCenterX(),CY  -  textBounds.exactCenterY(),油漆);
}
 

为什么不的高度()/ 2F 工作一样?

exactCentre() = (上+下)/ 2F

的高度()/ 2F = (下 - 上)/ 2F

这些只会产生相同的结果时, 0 。这可能对某些字体在所有大小,或在一些其它的尺寸字体的情况下,但不是对所有字体在所有大小

Target: Android >= 1.6 on a pure Canvas.

Suppose I want to write a function that will draw a (width, height) large red rectangle and then draw a black Hello World text inside. I want the text to be visually in the center of the rectangle. So let's try:

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

Now I don't know what to put in drawText's argument marked by ????, i.e. I don't know how to vertically align the text.

Something like

???? = topLeftY + height/2 + fontHeight/2 - fontHeight/8;

appears to work more or less ok, but there must be a better way.

解决方案

Example to centre on cx and cy:

private final Rect textBounds = new Rect(); //don't new this up in a draw method

public void drawTextCentred(Canvas canvas, Paint paint, String text, float cx, float cy){
  paint.getTextBounds(text, 0, text.length(), textBounds);
  canvas.drawText(text, cx - textBounds.exactCenterX(), cy - textBounds.exactCenterY(), paint);
}

Why doesn't height()/2f work the same?

exactCentre() = (top + bottom) / 2f.

height()/2f = (bottom - top) / 2f

These would only yield the same result when top is 0. This may be the case for some fonts at all sizes, or other fonts at some sizes, but not for all fonts at all sizes.

这篇关于如何垂直对齐文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆