Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and calculating the size of a one-line string in a given font and font size?

Is there an API method for calculating the size (i.e. width and height) of a string displayed on one line and in a given font and font size?

like image 692
SK9 Avatar asked Sep 07 '11 04:09

SK9


People also ask

How do I change font size in span tag?

As code, the scaling looks like this: Here the text is normal, <SPAN STYLE="font-size:18.0pt">here the text changes to 18.0pt,</SPAN> and here it returns to normal. Here the text is normal, <SPAN STYLE="font-size:18px">here the text changes to 18px,</SPAN> and here it returns to normal.

How do you fit text in TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.


1 Answers

Paint p = new Paint();
p.setTypeface(TypeFace obj); // if custom font use `TypeFace.createFromFile`
p.setTextSize(float size);
float textWidth = p.measureText("Your string"); 
// you get the width here and textsize is the height.
like image 106
Ronnie Avatar answered Sep 22 '22 15:09

Ronnie