News:

Happy New Year!

Main Menu

Variable width and constrained height

Started by miless2111s, January 08, 2025, 03:25:25 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

miless2111s

I want to be able to set the height of a rectangle and the width to vary based on the text that has been entered (with the user still able to control the font type and size).  This is analogous to the way that the flow chart shapes increase in height to accommodate the text, but keep the width constant.

So far I have been able to get close but I can't find a way to optimise the box; sometimes the width is too small so that the text slightly sticks out over the top and bottom and sometimes the width isn't as small as you would manually adjust it in terms of the text not being a neat block.

I have got txtWidth =TEXTWIDTH(TheText)/((Height-TopMargin-BottomMargin)/Char.Size)

I initially was using the pt->mm conversion of pt x 0.352778 = mm but I found that Visio had conversed the height to pt during the calculation so it was causing some very odd behaviour.

I have tended to have SpLine set to 100% as this generally seems to give better results than the default 120%.

Does anyone have any better solutions or builds on this method?

Thanks

PS I have cross posted to Stack Overflow in case our audiences don't overlap, I hope this doesn't cause any issues.

Yacine

Not sure what you want to achieve, could you please list your exact requirements?
Yacine

miless2111s

Yacine, thank you for your replay.

I want to have a box, of fixed height, into which I can type any length of text and the box width will adjust so that the text does not "spill" out of the top and bottom.

This can be achieved if the text is only on one line by making the width = textwidth(TheText) however this isn't ideal.  If the height would allow 2, 3 etc lines of text then it should do so without spilling over the top / bottom.

The method that I have come up with comes close, but doesn't quite do as good a job as you would do by manually adjusting the width, so I am hoping that someone can improve on it.

Yacine

No, we're not yet there.

There's so much we can do, we just need to define it properly.

We can make a variable depend on the text length. (in your case the width). But, is there a minimum width that you want to keep or reduce the width to zero if there's no text?
We can make others to be either completely fix, or adjustable manually. (in your case the height)
You speak about 2-3 lines. Shall the height adjust to this number of line or rather show an empty space when the text is shorter?.
Shall there be a maximum for the width?
What shall happen when the text length exceeds what the box can contain? 
- Spill over, 
- shrink the text size to adjust to the box size, 
- truncate the text with an "..." ending.
- add more lines?

That's what I meant by proper definition. You see, the possibilities are "almost" endless.
Yacine

miless2111s

Thank you for the additional guidance.   To answer the specific questions
* if there is no text the width reduces to 0
* the height is set by the user and can be adjusted, upon which the width will adjust automatically
* there can either be no max for the width or, if easy, the width should not place the left or right edge of the shape off the page.  If this happens the text should spill up and down, no text should be lost.
* The user should be able to enter line returns.  If this results in the user demanding more lines that the calculated max the text should spill up and down.
* The user should be able to govern the left/centre/right and top/centre/bottom placement of the text.
* The user can change the font size / type / bold etc and the width will shift as needed.

Medium priority items:
* Can the user be given the choice of how many rows will be used up to the max allowed by the height of the box? 

Low priority:
* Can the shape we aware of a width measurement (mm) in UserData1 and if the required width is greater than this value shift the shape to the left or right of the current location.  The default locPin Location is bottom left so if the user chooses left the LocPin shifts to x=Width*1, y=height*0 and if the user chooses right the LocPin shifts to x=0+UserData1

wapperdude

Visio does not have any mechanism to control the height of the actual text, nor limit the number of rows.  The means of controlling text height and/or number of rows are via char size and scaling, shape / textbox width.  This is, in my opinion, difficult to do even via code.

Setting a shape to 0 width is possible, but, I don't think it's a good idea.  Attached are a couple of shapes to explore. 

The 1st page is close to what you need.  There is:
1) a minimum height & width.  These are set in the width and height cells.  The MAX function does the limiting
2) the text line length will progress to the limit of the shape, and then line wrapping occurs.
3) the height will expand in response to char size and number of line wraps. 
4) text will always be constrained to be within the shape, and the shape cannot be sized to smaller than what is needed to contain the text.
5) the shape may be made larger than needed to contain the shape.  Increased width can reduce line wrapping and required text height.

The 2nd page has a different approach.  Namely, it uses font auto sizing to get best fit for given shape size.  Right click the shape to enable/disable sizing.  There are 2 sizing options.  Plus, the shape has many additional features as it was developed for a general application.
Visio 2019 Pro

miless2111s

Quote from: wapperdude on January 10, 2025, 04:18:33 AMVisio does not have any mechanism to control the height of the actual text, nor limit the number of rows.  The means of controlling text height and/or number of rows are via char size and scaling, shape / textbox width.  This is, in my opinion, difficult to do even via code.

Setting a shape to 0 width is possible, but, I don't think it's a good idea.  Attached are a couple of shapes to explore. 

The 1st page is close to what you need.  There is:
1) a minimum height & width.  These are set in the width and height cells.  The MAX function does the limiting
2) the text line length will progress to the limit of the shape, and then line wrapping occurs.
3) the height will expand in response to char size and number of line wraps. 
4) text will always be constrained to be within the shape, and the shape cannot be sized to smaller than what is needed to contain the text.
5) the shape may be made larger than needed to contain the shape.  Increased width can reduce line wrapping and required text height.

The 2nd page has a different approach.  Namely, it uses font auto sizing to get best fit for given shape size.  Right click the shape to enable/disable sizing.  There are 2 sizing options.  Plus, the shape has many additional features as it was developed for a general application.

Thnk you for these shapes, sadly it doesn't do what I need, which is to limit the height but there are some very interesting techniques (to put it mildly) which I can learn from.  On my laptop the 2nd shape was very laggy but that might be just my laptop showing its age :)

Yacine

#7
Hello Miles,
please find on page 3 the shape you wanted.
You cannot view this attachment.
Yacine

wapperdude

@Yacine:  If I understand the OP correctly, this doesn't do what he wants.  He really wants to control the maximum text height.  That is, he wants to limit the amount of line wrapping to a specific amount, e.g., 3 lines.  If the text needs to wrap more than these # of lines, then the shape width needs to extend to allow longer lines before the wrapping (line feed) occurs...thus maintaining no more than the maximum number of lines.  Visio accomplishes this when the User manually makes the shape wider...as per shape on page 1.  However, it seems he doesn't want to have to rely on User intervention.

I don't believe this is doable in shapesheet.  I think it is very difficult to do in VBA as it is somewhat of a circular problem.
Visio 2019 Pro

miless2111s

I've just discovered something which I am sure is old news to everyone else :)  You can set the height of the box to n*char.size.
Nothing much to do with the current issue, but it might be the start of an interesting idea I will develop a little more and report back on :)

Surrogate

Quote from: miless2111s on January 08, 2025, 03:25:25 PMI want to be able to set the height of a rectangle and the width to vary based on the text that has been entered (with the user still able to control the font type and size).  This is analogous to the way that the flow chart shapes increase in height to accommodate the text, but keep the width constant.
I tried to do something like this myself 15 years ago, but to no avail. ???

I think a drawing of the font anatomy might be useful in this thread....

Yacine

#11
Quote from: wapperdude on January 10, 2025, 05:36:54 PM@Yacine:  If I understand the OP correctly, this doesn't do what he wants.  He really wants to control the maximum text height.  That is, he wants to limit the amount of line wrapping to a specific amount, e.g., 3 lines.  If the text needs to wrap more than these # of lines, then the shape width needs to extend to allow longer lines before the wrapping (line feed) occurs...thus maintaining no more than the maximum number of lines.  Visio accomplishes this when the User manually makes the shape wider...as per shape on page 1.  However, it seems he doesn't want to have to rely on User intervention.

I don't believe this is doable in shapesheet.  I think it is very difficult to do in VBA as it is somewhat of a circular problem.
Now this is funny.
Miles was here after your comment, but did not answer on that.

I did actually put his request into the AI and it too understood, that the box is to grow first sidewise before wrapping the text downwards.

And before getting to the result I uploaded, I had a box that went first down, then to the side.

You cannot view this attachment.
Yacine

miless2111s

It is interesting that my initial results were based on playing with the TextWidth inside a box and it seemed sensible, but once I had coded up the specification it became clear that my thinking was muddled as the strict rules as written down lead to a single column of characters if the defined height of the box was say 4x font height.  This is obviously stupid :)

Yacine delivered what I requested; it worked well and fulfilled the brief.

However I think that what I was trying to do is a little more subtle than I had managed to specify.   I want a label that looks good when shown alongside a box, normally it would be a single line but in some situations it would want to be a multiple number of lines but NOT larger than the vertical dimension of the box it is labelling.

So as a result of all this I need to put my thinking cap on and play with some of the techniques that you have shown me and try to codify what I really want it to do :)  Hence the report back (probably later in the week or maybe at the end of this weekend).

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 366 (show)
Files included: 32 - 1207KB. (show)
Memory used: 1229KB.
Tokens: post-login.
Cache hits: 13: 0.00117s for 26,597 bytes (show)
Cache misses: 2: (show)
Queries used: 17.

[Show Queries]