Font size for a rect
-
Maybe I missed something so simple here. But I want to calculate the max size of a font given a rect. My solution is below. Is crap. I am sure there is a better way. I did a little extreme in the loop.
But a more streamlined func or a built in way to calculate the font_size most welcomed.
# coding: utf-8 import ui # only for testing... _inset = 0.0 _font_name = 'Avenir Next Condensed' _frame = ui.Rect(0,0, 320,480) def font_size_for_rect(rect, text, font_name, inset_factor = 0.0): fs_size = 0 # inset the rect, by a factor for width and height # the inset can be very helpful inset_rect = ui.Rect(rect.width * inset_factor, rect.height * inset_factor ) r = ui.Rect(*rect).inset(*inset_rect) for fs in xrange(0 , 1000): fw, fh = ui.measure_string(text, max_width=0, font=(font_name, fs), alignment=ui.ALIGN_LEFT, line_break_mode=ui.LB_TRUNCATE_TAIL) if fw > r.width or fh > r.height: fs_size = fs -1 break return fs_size if __name__ == '__main__': _frame = ui.Rect(0,0, 320, 480) v = ui.View(frame = _frame ) btn = ui.Button(frame = _frame) btn.title = '99' btn.bg_color = 'purple' fs = font_size_for_rect(_frame, '99', _font_name, inset_factor = _inset) print 'Calculated Font Size = ' , fs btn.font = (_font_name, fs) v.add_subview(btn) v.present('sheet')
-
doesn't a given text scale linearly with font size? If so, you could measure string with a nominal size, then simply compute the ratio needed to just fill the closest dimension, and apply that to the font size.