Scale text to fit in a box

时间:2015-06-15 14:24:04

标签: matlab

I'm working on a matlab GUI and I am trying to make the text font size resize to boxes. Currently I have a few solutions, but they aren't optimal. The fastest one is scaling the text by a goal/Extent factor:

goal = [0 0 .9 .9].*b.Position
fs = b.FontSize*min(goal./b.Extent)
b.FontSize = fs;

b is the uicontrol element. This works, but if the text is too long and needs to wrap it won't, and it will rather shrink the height until it fits on one line in the box.

I also have a while loop approach that * or /s by .99 until extent and goal are close enough.

What I've been looking for is an approach that will wrap the text so that I display the whole string but also take advantage of the whole space. I also had a recursive backtracking attempt that would try to find the best ratio the words would have to the ratio of the sides of the box, but that takes O(2^n) which is good to avoid.

Online I found the textwrap function, but I think that depends on font size, so it doesn't help me. Any ideas on how to do this?

1 个答案:

答案 0 :(得分:0)

maybe you can load your text as an image, instead of using labels use an axis and load the image o your desired text. Next time you wanted to resize your figure you won't be worry about it.