Calculating width of a string on Android

余生颓废 提交于 2019-12-07 10:12:54

问题


I need to know width of text string given font handle (or font name + font size + font style data).

in Windows I used to use GetTextExtentPoint()


回答1:


use this code

 Rect bounds = new Rect(); 
 Paint textPaint = textView.getPaint();
 textPaint.getTextBounds(text, 0, text.length(), bounds); 
 int height = bounds.height(); 
 int width = bounds.width();



回答2:


You can get TypeFace of TextView: http://developer.android.com/reference/android/widget/TextView.html#getTypeface() but it's hard to get font family, check also this: Check the family of a Typeface object in Android



来源:https://stackoverflow.com/questions/15355950/calculating-width-of-a-string-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!