Resizing Shapes with Text

3 4

Text associated with your shapes should look good after users edit the text or resize the shape. You can control text behavior and appearance with formulas that correlate shape geometry and text. You add text formulas by editing the cells in the ShapeSheet window.

This section describes how to control the size of a shape's text block as a user types in it, base a shape's size on either the amount or value of its text, and proportionately change a shape's font as the shape is resized.

Controlling Text Block Size

When you customize text block behavior, you'll often want the text block to expand as users add text. You can add formulas that control text block size by setting the initial boundaries of the text block and then ensuring that the text block can resize to encompass added text.

To control the text block size, use the max function to define the maximum allowable size and the textwidth and textheight functions, which evaluate the width and height of the composed text (theText) in a shape. Add these formulas to the shape's Text Transform section to create a text block that expands as the user types more text:

 TxtWidth = MAX(TEXTWIDTH(theText), 8 * Char.Size) TxtHeight= TEXTHEIGHT(theText, TxtWidth) 

Note


To view the Text Transform section, open the ShapeSheet window for the shape or master. On the Insert menu, click Section, select Text Transform, and then click OK.

You can modify the previous formula, or enter your own formulas to specify different behavior.

Controlling text block width

By default, the width of the text block is set to whichever value is greater: the longest text line terminated by a hard return, or eight times the font size (which ensures that the text block is at least wide enough to hold a word or two). If the text block contains text formatted with more than one font size, this formula uses the size of the first font used in the text block at the time the formula was created.

To tie the width of a text block to a variable You can enter formulas that tie the width of a text block to a different variable, such as the width of the shape or the font size of the text, or you can specify a fixed width for the text block. By default, the text block width is the same as the width of the shape. If you wanted the text block to be half the width of the shape instead, you would enter this formula in the Text Transform section of the shape's sheet:

 TxtWidth = Width/2 

Or, if you wanted the text block to be 20 times the width of the font size, you would enter this formula:

 TxtWidth = 20 * Char.Size 

To set a minimum text block width You can also set a text block's width to a minimum size by using the min function. For example, this formula ensures that when a shape is resized, its text block doesn't stretch wider than 4 inches or resize narrower than 0.5 inch:

 TxtWidth = MIN(4 in., MAX(0.5 in., Width)) 

In this case, the value for maximum text block width will be the larger of either 0.5 inch or the width of the shape. The minimum width for the text block is calculated in turn by comparing the maximum value with 4 inches, and choosing whichever is smaller.

To prevent a text block from resizing If you want to prevent the text block from resizing if the shape is resized, you can enter a fixed width for the text block. For example, to set a text block's width to 2 inches, you would enter this formula:

 TxtWidth = 2 in. 

Controlling text block height

Normally, you want the height of a text block to expand when users add text to it. When you're designing the behavior of text blocks, you want to build in as much flexibility as possible so users aren't prevented from adding text to a shape. By default, TxtHeight is equal to the height of the shape. Adding the following TxtHeight formula returns the height of the shape's composed text where no text line exceeds TxtWidth:

 TxtHeight = TEXTHEIGHT(theText, TxtWidth) 

This formula returns a value that represents the height of the text in the shape, including line spacing and space before and after each paragraph in the block, assuming that no line in the block is longer than the maximum value for TxtWidth. This formula delivers good results in most cases, allowing the height of the text block to expand as users add text.

Basing Shape Size on the Amount of Text

You can create a shape whose size depends on the amount of text it contains. If you want a shape that is just big enough to fit the text typed into it, such as a word balloon or text callout shape, use the textwidth and textheight functions as part of the formulas for the shape's width and height.

For example, the following formula in the Shape Transform section limits a shape's width to the length of the text lines it contains plus a small margin:

 Width = GUARD(TEXTWIDTH(theText) + 0.5 in.)  

The function returns the width of all the text in the shape (theText). The shape's width is limited to that value plus 0.5 inch; when the text block is empty, the shape's width is 0.5 inch. The guard function prevents the user from stretching the shape's width with selection handles, which would cause new values to overwrite the formula in the Width cell. To make it more obvious to users that they cannot stretch the shape manually, you could also set the LockWidth cell in the Protection section.

This related formula limits a shape's height to the number of lines of text it contains:

 Height = GUARD(TEXTHEIGHT(theText,Width)) 

Tip


The TEXTWIDTH and TEXTHEIGHT functions cause Microsoft Visio to recompose the shape's text with each keystroke. To maximize performance, you can include a minimum-size test in your formula so the text grows only after the text reaches a given width or height. Beyond that width or height, Visio still must recompose the text with each keystroke. For example, you can create a 2-inch by 0.5-inch box that grows in height to accommodate additional text. To offset potential performance problems, the box doesn't resize until the text height reaches 0.5 inch. To create this behavior, add these formulas to the Shape Transform section:
 Width = 2 in.  Height = GUARD(MAX(.5 in. TEXTHEIGHT(TheText, _      Width)))  

Basing Shape Size on Text Value

You can create a shape whose size is controlled by the value of the text it contains. For example, in a bar chart, you can ensure that the size of a bar depends on the value it represents. With the EVALTEXT function, you can create simple charting shapes or other shapes into which users type a value that determines the shape's width or height. For example, to associate a shape's width with its text value, put the following formula in the Shape Transform section:

 Width = GUARD(EVALTEXT(TheText)) 

The EVALTEXT function evaluates the text in the shape's text block as if it was a formula and returns the result. For example, if you type 10 cm, the shape's width changes to 10 cm. If there is no text or the text cannot be evaluated—for example, a nonnumeric value is typed—the shape's width is zero. You can further refine the shape by resizing it only in the direction of growth, such as for a bar that grows to the right. To do this, use the Rotation tool to move the shape's pin to the stationary end.

Changing the Font Size as a Shape is Resized

By default, when a user resizes a shape, its geometry and text block change, but the font size does not. You can make font size a function of shape geometry by writing formulas in the Character section of the ShapeSheet. The formulas discussed in this section adjust only the character size. If you want to change text indents or line spacing, you must use similar formulas in the cells that control those attributes.

Note


If a shape is to be used in scaled drawings, you should take the drawing scale into account when you make font size a function of shape height.

Writing a Formula to Resize Text

You can make font size a function of a shape's size so that when a user resizes the shape, its text increases in proportion to the value of its height. To create a shape whose size and text font size resize proportionately, use the following general formula in the shape's Character section:

 Size = (Height/<original height>) * (<original font size>) 

For example:

 Size = (Height/1 in.) * 12pt 

The third value in the formula is a ratio derived from dividing the original text size by the height of the shape. For example, if you use the Triangle shape from the Basic Shapes stencil at its default size, this value will range from 0.037 for text that was originally 4 points to 1.1852 for text that was originally 128 points.

To improve shape performance, you could store the proportional formula in a user-defined cell. For example, assume the original shape height is 3 cm and the original font size is 10 pt. Insert the User-Defined Cells section in the ShapeSheet window, and then add the following formulas:

 User.FontSize     = Height/3cm * 10pt Char.Size         = User.FontSize 

Note


To insert the User-Defined Cells section in the ShapeSheet window: Select the shape, click Show ShapeSheet on the Window menu, click Section on the Insert menu, select the User-defined cells check box, and then click OK.

If you want to ensure that the font size is always readable, you can limit the range of acceptable sizes. For example, to limit font size from 4 to 128 points, you would use the min and max functions in conjunction with the proportional formula previously mentioned:

 User.FontSize = MIN(128pt, MAX(4pt, Height/3cm * 10pt)) 

For details about the min and max formula syntax, see the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

Be sure to use minimum and maximum font sizes that are supported by the expected printers and drivers. To ensure consistency if the Character section for a shape contains more than one row, the Size cells in subsequent rows should use similar formulas.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net