1

i've looked for solutions of auto-fitting the font of the textView according to its size , and found many , but none support multi-lines and does it correctly (without truncating the text and also respect the gravity values) .

has anyone else conducted a solution as such?

is it also possible to set the constraint of how to find the optimal number of lines ? maybe according to max font size or max characters numbers per line?

halfer
  • 19,824
  • 17
  • 99
  • 186
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • It would be helpful if you list the solutions that you have tried and why each doesn't work for you to save everyone's time :) – Joe Aug 30 '12 at 16:21
  • as i've written , i've tried at least 5 solutions on this website , and none supported multiline,use gravity AND show the text correctly – android developer Aug 30 '12 at 20:06
  • In case you missed it, my suggestion is to **list** the solutions that you have tried. – Joe Aug 31 '12 at 23:44
  • i don't remember which i've tried . i've tried at least 5 of them . here are some links i remember that i've visited: http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds http://stackoverflow.com/questions/2596452/how-to-scale-resize-text-to-fit-a-textview – android developer Sep 01 '12 at 09:15

2 Answers2

3

The following is working for me, IMHO better, than using a ellipsize based solution.

void adjustTextScale(TextView t, float max, float providedWidth, float providedHeight) {

    // sometimes width and height are undefined (0 here), so if something was provided, take it ;-)
    if (providedWidth == 0f)
        providedWidth = ((float) (t.getWidth()-t.getPaddingLeft()-t.getPaddingRight()));
    if (providedHeight == 0f)
        providedHeight = ((float) (t.getHeight()-t.getPaddingTop()-t.getPaddingLeft()));

    float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());

    String[] lines = t.getText().toString().split("\\r?\\n");

    // ask paint for the bounding rect if it were to draw the text at current size
    Paint p = new Paint();
    p.setTextScaleX(1.0f);
    p.setTextSize(t.getTextSize());
    Rect bounds = new Rect();
    float usedWidth = 0f;

    // determine how much to scale the width to fit the view
    for (int i =0;i<lines.length;i++){
        p.getTextBounds(lines[i], 0, lines[i].length(), bounds);
        usedWidth = Math.max(usedWidth,(bounds.right - bounds.left)*pix);
    }

    // same for height, sometimes the calculated height is to less, so use §µ{ instead
    p.getTextBounds("§µ{", 0, 3, bounds);
    float usedHeight = (bounds.bottom - bounds.top)*pix*lines.length;

    float scaleX = providedWidth / usedWidth;
    float scaleY = providedHeight / usedHeight;

    t.setTextSize(TypedValue.COMPLEX_UNIT_PX,t.getTextSize()*Math.min(max,Math.min(scaleX,scaleY)));


}
Xnyle
  • 83
  • 10
0

Later I've asked a similar question and got an answer to this one:

https://stackoverflow.com/a/17786051/878126

for the custom behaviour, one need to change the code accordingly .

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270