Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure out width of a String in a certain Font

Is there any way to figure out how many pixels wide a certain String in a certain Font is?

In my Activity, there are dynamic Strings put on a Button. Sometimes, the String is too long and it's divided on two lines, what makes the Button look ugly. However, as I don't use a sort of a console Font, the single char-widths may vary. So it's not a help writing something like

String test = "someString";
if(someString.length()>/*someValue*/){
    // decrement Font size
}

because an "mmmmmmmm" is wider than "iiiiiiii".

Alternatively, is there a way in Android to fit a certain String on a single line, so the system "scales" the Font size automatically?

EDIT:

since the answer from wsanville was really nice, here's my code setting the font size dynamically:

private void setupButton(){
    Button button = new Button();
    button.setText(getButtonText()); // getButtonText() is a custom method which returns me a certain String
    Paint paint = button.getPaint();
    float t = 0;
    if(paint.measureText(button.getText().toString())>323.0){ //323.0 is the max width fitting in the button
    t = getAppropriateTextSize(button);
    button.setTextSize(t);
    }
}

private float getAppropriateTextSize(Button button){
    float textSize = 0;
    Paint paint = button.getPaint();
    textSize = paint.getTextSize();
    while(paint.measureText(button.getText().toString())>323.0){
        textSize -= 0.25;
        button.setTextSize(textSize);
    }
    return textSize;
}
like image 708
Valentino Ru Avatar asked Oct 30 '12 13:10

Valentino Ru


People also ask

How do you find the width of a string?

stringWidth to find the width for the specified string. For example, if you have a Graphics variable called g , you'd use: int width = g. getFontMetrics().

How to find width of String in js?

The clientWidth property is used to get the width of its element. This value will represent the width of text in pixels. The element is then removed using the removeChild() method. Method 2: Using the canvas measureText() method: A new “canvas” element is created with the createElement() method.

How is font width calculated?

A font is often measured in pt (points). Points dictate the height of the lettering. There are approximately 72 (72.272) points in one inch or 2.54 cm. For example, the font size 72 would be about one inch tall, and 36 would be about a half of an inch.

How do I measure text size?

Point size measures from the height of the highest ascender (peak) to the baseline of the lowercase x. It then measures from the lowest descender (valley) of the font to the top of the lowercase x. Standardized fonts (e.g. Arial, Times New Roman, Calibri, etc.)


1 Answers

You should be able to use Paint.setTypeface() and then Paint.measureText(). You'll find other methods on the Paint class like setTextSize() to help too.

Your followup question about scaling text was addressed in this question.

like image 154
wsanville Avatar answered Oct 23 '22 06:10

wsanville