Android自定义View时获取文字宽高

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u014112893/article/details/78555172

获取文字宽度:

private int getTextWidth(String text, Paint paint) {
    Rect rect = new Rect(); // 文字所在区域的矩形
    paint.getTextBounds(text, 0, text.length(), rect);
    return rect.width();
}

获取文字高度:

private int getTextHeight(String text, Paint paint) {
    Rect rect = new Rect();
    paint.getTextBounds(text, 0, text.length(), rect);
    return rect.height();
}

猜你喜欢

转载自blog.csdn.net/u014112893/article/details/78555172
今日推荐