获取文字高度,包括字体大小和设置高度 [英] Get the text height including the font size and set that height
问题描述
我有一个叫文您好
现在我需要说12或18,只要我们应用字体的文本的文本大小增加申请字号此了。
i have a text called Hello
now i need to apply fontsize for this say 12 or 18 now as soon as we apply font to the text the text size increases.
所以现在我需要获取文本的高度,包括使用油漆的字体大小。
so now i need to get the text height including the font size using paint.
我曾尝试与油漆以下内容:
i have tried with paint the following:
String finalVal ="Hello";
Paint paint = new Paint();
paint.setTextSize(18);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
// Measure the text rectangle to get the height
paint.getTextBounds(finalVal, 0, finalVal.length(), result);
但它不工作,请大家帮忙
But its not working , please help
修改
我想设置的WebView的动态基础上,textHeight不同我得到单行文本高度类似的高度你好
但如果有两行文字我的名字是ABC和我爸爸的名字是某某和我妈妈的名字是123现在它没有得到正确的文本高度。
i am trying to set the height of webview dynamically based on the textheight i am getting the text height for single line like "Hello"
but if there are two lines in the text "My name is abc and my dads name is xyz and my moms name is 123" now its not getting the proper text height".
推荐答案
试试这个方法:
String finalVal ="Hello";
Paint paint = new Paint();
paint.setTextSize(18);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
paint.getTextBounds(finalVal, 0, finalVal.length(), result);
Log.d("WIDTH :", String.valueOf(result.width()));
Log.d("HEIGHT :", String.valueOf(result.height()));
下面是输出:
WIDTH : 40
HEIGHT : 14
如果我设置,
String finalVal ="My name is abc and my dads name is xyz and my moms name is 123";
我的输出是:
WIDTH : 559
HEIGHT : 18
感谢。
这篇关于获取文字高度,包括字体大小和设置高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!