Auto-Resize FormattedString



  • Hi,

    I'm looking for a solution to find the maximum value of FontSize that can be set to allow a FormattedString() to occupy all the available space in a TextBox.

    For TextBox using a simple string, I have already coded this snippet that returns the maximum fontSize value:

    def get_max_fontSize_textBox(font, text, box, align=None, fS=200):
    	font(FONT)
    	fontSize(fS)
    	hyphenation(False)
    	overflow = textOverflow(text, box, align)
    	while overflow:
    		font(FONT)
    		fS -= 1
    		fontSize(fS)
    		overflow = textOverflow(text, box, align)
    return fS
    

    I would like to achieve exactly the same thing, but using FormattedString() instead of a str.

    The problem is how to build again the FormattedString() in the while overflow loop.


  • admin

    why trying to find the pointSize? just scale everything with textWidth/boxWidth



  • I hadn't thought about this solution!
    Thanks, @frederik!

    newPage("A4Landscape")
    
    box_w, box_h = width()-100, height()-100
    box_x, box_y = 50, 50
    
    txt = FormattedString()
    
    txt.fontSize(90)
    txt.align("center")
    txt.append("LOOOOOOOOOOOOOOONG STRING\n"*30)
    
    txt_w = textSize(txt)[0]
    txt_h = textSize(txt)[1]
    
    width_ratio = box_w/txt_w 
    height_ratio = box_h/txt_h
    
    sf = min(width_ratio , height_ratio, 1)
    
    scale(sf)
    textBox(txt, (box_x/sf, box_y/sf, box_w/sf, box_h/sf))
    

Log in to reply