| Create Canvas Documents with C1FlashCanvas > Adding Text to C1FlashCanvas |
This topic demonstrates how to add text to your Flash document.
Adding text to C1FlashCanvas is easy; all the work is done by the DrawString method.
DrawString draws a given string at a specified location using a given font and brush. For example:
canvas.DrawString("Hello World!", font, Brushes.Black, rc);
By default, DrawString will align the text to the left and to the top of the given rectangle and will wrap the string within the rectangle. You can change these options by specifying a StringFormat parameter in the call to DrawString. The StringFormat has members that allow you to specify the horizontal alignment (Alignment), vertical alignment (LineAligmnent), and flags that control wrapping and clipping.
For example, the following code creates a StringFormat object and uses it to align the text to the center of the rectangle horizontally:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Dim font As New Font("Arial", 12)
Dim rc As New RectangleF(72, 72, 100, 50)
Dim [text] As String = "Some long string to be " + "rendered into a small rectangle. "
[text] = [text] + [text] + [text] + [text] + [text] + [text]
' center align string
Dim sf As New StringFormat()
sf.Alignment = StringAlignment.Center
canvas.DrawString([text], font, Brushes.Black, rc, sf)
canvas.DrawRectangle(Pens.Gray, rc)
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
Font font = new Font("Arial", 12);
RectangleF rc = new RectangleF(72, 72, 100, 50);
string text = "Some long string to be " +
"rendered into a small rectangle. ";
text = text + text + text + text + text + text;
// center align string
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
canvas.DrawString(text, font, Brushes.Black, rc, sf);
canvas.DrawRectangle(Pens.Gray, rc);
|
|
In many cases, you will need to check whether the string will fit on the page before you render it. You can use the MeasureString method for that. MeasureString returns a SizeF structure that contains the width and height of the string (in points) when rendered with a given font.
DrawString provides all the functionality you need for rendering paragraphs using a single font and color.
You can also use DrawStringHtml to render a limited subset of the HTML tag language with a few additions not normally present in HTML. The following tags are supported:
| Tag | Description |
|---|---|
| <p> ... </p> | Defines a paragraph. The attribute align may be present, with value left, right, or center. |
| <br> | Inserts a line break. |
| <a> ... </a> | Defines a hyperlink. The attribute href must be present. The attribute target is optional, and specifies a window name. |
| <font> ... </font> |
Defines a span of text that uses a given font. The following attributes are available:
|
| <b> ... </b> | Defines a span of bold text. |
| <i> ... </i> | Defines a span of italic text. |
| <u> ... </u> | Defines a span of underlined text. |
| <li> ... </li> | Defines a bulleted paragraph. The <ul> tag is not necessary and is not supported. Numbered lists are not supported. |
| <textformat> ... </textformat> |
Defines a span of text with certain formatting options. The following attributes are available:
|
| <tab> | Inserts a tab character, which advances to the next tab stop as defined with <textformat> |
For example, the following code renders a line of text with some bold and italic characters in it, and the "Sample" is in green:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Dim font As New Font("Arial", 12)
Dim rect As New RectangleF(160, 120, 200, 60)
canvas.DrawStringHtml("<b><font color=""#00FF00"">Sample</font></b> <i>string</i>", font, Brushes.Red, rect)
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
Font font = new Font("Arial", 12);
Rectangle rect = new RectangleF(160, 120, 200, 60);
canvas.DrawStringHtml("<b><font color=\"#00FF00\">Sample</font></b> <i>string</i>", font, Brushes.Red, rect);
|
|