Android自定義View時獲取文字寬高

獲取文字寬度:

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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章