The text displayed by a shape is represented by the NTextBlock class an instance of which can be obtained from the TextBlock property of each shape. The currently displayed text is controlled by the Text property of the text block. The shape also has a shortcut Text property, which allows you to display a string inside any shape like this:
Associating text with a shape |
Copy Code
|
NShape shape;
// ...
shape.Text = "NOV Diagram Rocks";
|
The text block has its own coordinate system. This means that the text block content is transformed with a certain transformation prior to be displayed.
A common requirement is to move the text block of a shape below its geometry. To do that, after setting the text of the shape, you should call the MoveTextBlockBelowShape of the NShape class:
Move the text block of a shape below its geometry |
Copy Code
|
shape.Text = "My Shape";
shape.MoveTextBlockBelowShape();
|
The properties that control the text block transformation can be logically separated in three categories:
In order to function properly the text block transformation needs to be initialized first. The InitXForm method helps you properly initialize the transformation and resize behavior of the text block. The method takes a single parameter from the ENTextBlockXForm enumeration. Following is a description of the available options:
ShapeBox
Sets the ResizeMode property to ShapeSize (i.e. resizes the text block to the shape).
Binds the pin to the middle of the shape.
Sets the Angle to zero.
ShapeLineMiddle
Sets the ResizeMode property to TextSize (i.e. resizes the text block to the size of its text).
Binds the pin to the middle of the shape line (the line connecting the Begin and End points of the shape).
Binds the angle to the shape line angle.
GeometryContourMiddle
Sets the ResizeMode property to TextSize (i.e. resizes the text block to the size of its text).
Binds the pin to the middle of the geometry contour (outline).
Binds the angle to the shape geometry contour angle in the middle.
CustomPin
Sets the ResizeMode property to TextSize (i.e. resizes the text block to the size of its text).
Leaves the PinX and PinX to be specified by the user.
Sets the angle to zero.
All types of text block transform bind the local pin point to the text block center.
If the shape transform is not explicitly initialized, NOV Diagram will perform automatic transform initialization, when the text block element is requested for the first time. The transform initialization is different for 1D and 2D shapes:
-
for 2D shapes - initializes the text transform as the ShapeBox option.
-
for 1D shapes - if the geometry of the shape has closed figures, initializes the text transform with the ShapeLineMiddle option. Otherwise initializes the text transform with the GeometryContourMiddle option.
In many cases it is required to keep the text displayed by the text block with upward orientation. When the text block KeepUpward property is set to true, NOV Diagram internally applies such a transform to the text, so that its Up vector is always oriented towards the top of the screen. This greatly increases the readability of diagrams.
The text block Width and Height control size of the text block. The Width and Height of the text block can be automatically computed based on the ResizeMode property, that accepts values from the ENTextBlockResizeMode enumeration:
-
None - the text block is not automatically resized. It is up to the developer to specify either a fixed text block size or write expressions that define the text block Width and Height.
-
ShapeSize - the text block Width and Height are bound to the owner shape Width and Height (i.e. the text block is resized to the shape).
-
TextSize - the text block Width and Height are bound to the text block DesiredWidth and DesiredHeight, that are automatically calculated to the measured size of the text block (i.e. the text block is sized to the natural size of its content).
The text block DesiredWidth and DesiredHeight properties expose the natural desired size of the text block regardless of its resize mode. In this way it is possible to use this information to make a shape that is sized to its text block - simply bind the shape Width and Height to the text block DesiredWidth and DesiredHeight via expressions.
Below is a reference of the properties that affect the appearance of the text block:
Property |
Description |
Font and Font Style
|
FontName |
Controls the font name used to display the text. |
FontSize |
Specifies the size of the font. |
FontStyle
|
Specifies the font style (e.g. whether the font is bold, italic underlined etc.) |
Text Layout
|
HorizontalAlignment |
Controls the horizontal alignment of the text inside the text block. |
VerticalAlignment |
Controls the vertical alignment of the text inside the text block. |
WrapMode |
Specifies the behavior of the text block when the text needs to be wrapped (goes outside the text block bounds). Possible options are word wrap, display ellipses and clip. |
BaseLine |
Controls the base line of the text (e.g. normal, subscript, superscript) |
Box Model
|
Padding |
Controls the padding applied to the text inside the text block. |
BorderThickness |
Controls the thickness of the text border border. |
Styles
|
Fill |
Specifies the filling of the text. |
Stroke |
Specifies the stroking of the text outlines. |
Shadow |
Specifies the shadow dropped by the text. |
Border |
Specifies the appearance of the text block border. |
BackgroundFill |
Specifies the filling of the text block background. |
The text block also supports rich formatted text which means that it can display text with mixed font style, font size, appearance etc. In fact the text block content has the full functionality of the Nevron NOV Rich Text control except the support for sections. The following code snippet shows how to add rich formatted text to a text block:
Add rich formatted text to a text block |
Copy Code
|
NParagraph paragraph = new NParagraph();
NTextInline textInline1 = new NTextInline("This paragraph contains text inlines with altered ");
paragraph.Inlines.Add(textInline1);
NTextInline textInline2 = new NTextInline("Font Name, ");
textInline2.FontName = "Tahoma";
paragraph.Inlines.Add(textInline2);
NTextInline textInline3 = new NTextInline("Font Size, ");
textInline3.FontSize = 14;
paragraph.Inlines.Add(textInline3);
NTextInline textInline4 = new NTextInline("Font Style (Bold), ");
textInline4.FontStyle |= ENFontStyle.Bold;
paragraph.Inlines.Add(textInline4);
NTextInline textInline5 = new NTextInline("Font Style (Italic), ");
textInline5.FontStyle |= ENFontStyle.Italic;
paragraph.Inlines.Add(textInline5);
NTextInline textInline6 = new NTextInline("Font Style (Underline), ");
textInline6.FontStyle |= ENFontStyle.Underline;
paragraph.Inlines.Add(textInline6);
NTextInline textInline7 = new NTextInline("Font Style (StrikeTrough) ");
textInline7.FontStyle |= ENFontStyle.Strikethrough;
paragraph.Inlines.Add(textInline7);
NTextInline textInline8 = new NTextInline("and Font Style All.");
textInline8.FontStyle = ENFontStyle.Bold | ENFontStyle.Italic | ENFontStyle.Underline | ENFontStyle.Strikethrough;
paragraph.Inlines.Add(textInline8);
someShape.TextBlock.Content.Blocks.Clear();
someShape.TextBlock.Content.Blocks.Add(paragraph);
|
For a full list of the features supported by Nevron NOV Rich Text control please consult with the control documentation.