android - Calculate text size according to width of text area -
i have text should set textview specified width. needs calculate text size fit textview.
in other words: there way fit text textview area, imageview scale type feature?
if size of space text takes after following might help:
paint paint = new paint(); rect bounds = new rect(); int text_height = 0; int text_width = 0; paint.settypeface(typeface.default);// preference here paint.settextsize(25);// have same text size string text = "some random text"; paint.gettextbounds(text, 0, text.length(), bounds); text_height = bounds.height(); text_width = bounds.width();
edit (after comment): use above in reverse:
int text_height = 50; int text_width = 200; int text_check_w = 0; int text_check_h = 0; int incr_text_size = 1; boolean found_desired_size = true; while (found_desired_size){ paint.settextsize(incr_text_size);// have same text size string text = "some random text"; paint.gettextbounds(text, 0, text.length(), bounds); text_check_h = bounds.height(); text_check_w = bounds.width(); incr_text_size++; if (text_height == text_check_h && text_width == text_check_w){ found_desired_size = false; } } return incr_text_size; // desired text size bounds have
//this method may tweaked bit, gives idea on can do
Comments
Post a Comment