One reason why the original code didn't work is that ui doesn't let you set the anchor point -- if you want the center to be the anchor, you need to set the center again after calling size_to_fit.