Center custom Font with canvas.DrawText()

时间:2015-06-30 13:47:11

标签: android xamarin font-awesome

I have a custom font String (Font-Awesome Icon) and I want to draw it into a FloatingActionButton. For this reason I must convert the String to a Drawable like this. The problem is that the icon is never centered, even if I try something like this:

public override void Draw(global::Android.Graphics.Canvas canvas)
{
    Rect bounds = new Rect();
    paint.GetTextBounds(text, 0, text.Length, bounds);
    float x = (canvas.Width / 2f) - (bounds.Width() / 2f)
    float y = (canvas.Height / 2f) - (bounds.Height() / 2f);
    canvas.DrawText(text, x, y, paint);
}

The result will be something like this:

The icon on the FloatingActionButton is not centered

I can't figure out the problem.

Thanks in advance.

Edit: If I do GetBounds, this is the result:

Result

0 个答案:

没有答案