f



Fit text in a Text Box

Hello friends,
I have a curious doubt, imagine I have a text box, and inside, a text that I dont know the lenght. Depending on the box's size and letter's size, I can fit the box. But the box's size is fixed, so I just can modify the size's letter. How can I be right with letter's size, if I dont know the lenght of string (text)? Is there any simple function for this case?
Thanks in advance, greetings.
Pedro Moreno
0
pedro
8/31/2010 9:25:09 PM
comp.soft-sys.matlab 211266 articles. 22 followers. lunamoonmoon (257) is leader. Post Follow

6 Replies
2160 Views

Similar Articles

[PageSpeed] 45

On 10-08-31 04:25 PM, pedro moreno wrote:

> I have a curious doubt, imagine I have a text box, and inside, a text
> that I dont know the lenght. Depending on the box's size and letter's
> size, I can fit the box. But the box's size is fixed, so I just can
> modify the size's letter. How can I be right with letter's size, if I
> dont know the lenght of string (text)? Is there any simple function for
> this case?

Temporarily set the width of the text uicontrol to bigger than you could 
possibly need, and start with the desired FontSize. get() the control's String 
property and store that in a variable. Now,

[junk, recommended] = textwrap(TextboxHandle, DesiredString);

recommended(3) will now be the width needed to fit the string taking in to 
account whether font is proportional or not and taking in to account the font 
size.

The ratio of recommended(3) to the current text uicontrol's width will be the 
ratio by which you need to scale the font size for a fit... but don't be 
surprised if there is a pixel difference one way or the other. After setting 
the new FontSize you could always repeat the sizing test to see if a further 
adjustment is warranted. Probably if recommended(3) is less than the current 
width (and thus the string fits already) you would not want to alter the font 
size.
0
roberson (2980)
8/31/2010 9:52:30 PM
Ufff... It looks really complicated... I'm nnot sure how I can use the instruction textwrap... But I put this:

>>GoodLetterSize=textwrap("Message to put in handles.tex6 (the text box)",8)

8 is the size letter that i used for it, but days ago I saw that for some cases message was so long for text static box. And it says to me that GoodLetterSize is [1 2 2 1], what does it mean?
Very complicated the problem that I think... Anyway, thx Walter :)
0
pedro
9/1/2010 11:16:07 AM
On 01/09/10 6:16 AM, pedro moreno wrote:
> Ufff... It looks really complicated... I'm nnot sure how I can use the
> instruction textwrap... But I put this:
>
>>> GoodLetterSize=textwrap("Message to put in handles.tex6 (the text
>>> box)",8)
>
> 8 is the size letter that i used for it, but days ago I saw that for
> some cases message was so long for text static box. And it says to me
> that GoodLetterSize is [1 2 2 1], what does it mean?
> Very complicated the problem that I think... Anyway, thx Walter :)

The first argument to textwrap() has to be a uicontrol, and the second 
argument has to be the text to be wrapped to fit inside the control. If 
8 is your fontsize, you would have to set() the uicontrol to have that 
FontSize before you did the textwrap()

You fed nonsense arguments to textwrap() and I'm surprised you didn't 
just get an error.
0
Walter
9/7/2010 7:15:13 AM
I tried both the recommended method and the Matlab example (the recommedned method being consistent with the documntation).  Neither worked.

There were no error message or anything.
i.e. example
     MsgHandle = uicontrol('style', 'text', 'position', [20 20 100 100]);
        MsgString = {'This is a long message string that needs to be wrapped'};
        WrapString=textwrap(MsgHandle,MsgString);

the uicontrol is displayed but without the string.

Walter Roberson <roberson@hushmail.com> wrote in message <i5jtlh$kn5$1@canopus.cc.umanitoba.ca>...
> On 10-08-31 04:25 PM, pedro moreno wrote:
> 
> > I have a curious doubt, imagine I have a text box, and inside, a text
> > that I dont know the lenght. Depending on the box's size and letter's
> > size, I can fit the box. But the box's size is fixed, so I just can
> > modify the size's letter. How can I be right with letter's size, if I
> > dont know the lenght of string (text)? Is there any simple function for
> > this case?
> 
> Temporarily set the width of the text uicontrol to bigger than you could 
> possibly need, and start with the desired FontSize. get() the control's String 
> property and store that in a variable. Now,
> 
> [junk, recommended] = textwrap(TextboxHandle, DesiredString);
> 
> recommended(3) will now be the width needed to fit the string taking in to 
> account whether font is proportional or not and taking in to account the font 
> size.
> 
> The ratio of recommended(3) to the current text uicontrol's width will be the 
> ratio by which you need to scale the font size for a fit... but don't be 
> surprised if there is a pixel difference one way or the other. After setting 
> the new FontSize you could always repeat the sizing test to see if a further 
> adjustment is warranted. Probably if recommended(3) is less than the current 
> width (and thus the string fits already) you would not want to alter the font 
> size.
0
10/28/2010 3:07:04 PM

"Stephen Gay" <Stephen_Gay@sgs.com> wrote in message 
news:iac3io$h10$1@fred.mathworks.com...
>
> I tried both the recommended method and the Matlab example (the 
> recommedned method being consistent with the documntation).  Neither 
> worked.
>
> There were no error message or anything.
> i.e. example
>     MsgHandle = uicontrol('style', 'text', 'position', [20 20 100 100]);
>        MsgString = {'This is a long message string that needs to be 
> wrapped'};
>        WrapString=textwrap(MsgHandle,MsgString);
>
> the uicontrol is displayed but without the string.

That's correct.  TEXTWRAP just wraps the string; it doesn't actually store 
the string in the uicontrol.  Add:

set(MsgHandle, 'String', WrapString)

to the end of the code above and it should display the string appropriately 
wrapped.

*snip*

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Steven_Lord
10/28/2010 5:43:43 PM
Dear Pedro,

I do not understand, why the problem should be processed by TEXTWRAP. 
You have a text box with a fixed width and want to fit a single line of text - correct?
  Width = 200;
  H = uicontrol('Style', 'text', 'Position', [10, 10, Width, 20], ...
      'String', 'A long line which might exceed the available space', ...
      'BackgroundColor', ones(1, 3));
  for FontSize = [22, 20, 18, 16, 14, 12, 10, 9, 8, 7]
     set(H, 'FontSize', FontSize);
     Extent = get(H, 'Extent');
     if Extent(3) < Width
        break;  % Escape loop: for FontSize
     end
  end

This does no line-wrapping, but shrinks the fontsize until the text ist smaller than the UICONTROL.

Jan
0
Jan
10/28/2010 7:31:03 PM
Reply: