76 строки
3.6 KiB
Plaintext
76 строки
3.6 KiB
Plaintext
---
|
|
title: "Creating a TextBlock"
|
|
isMarkdown: false
|
|
---
|
|
<h1 id="creating-a-textblock">Creating a TextBlock</h1>
|
|
<p>The TextBlock class is a low-level class for working with a single block of text. In
|
|
general you probably don't need to use this class as the higher level RichString class
|
|
is easier to work with and provides most of the same capabilities.</p>
|
|
<p>Never-the-less, this section describes how to work with the TextBlock class.</p>
|
|
<h2 id="create-a-text-block">Create a Text block</h2>
|
|
<p>To create a text block, simply create a new instance of the TextBlock class. You'll
|
|
probably also want to set some layout properties like the maximum width and the text alignment:</p>
|
|
<pre class="language-csharp"><code class="language-csharp">// You'll need this namespace
|
|
using Topten.RichTextKit;
|
|
|
|
// Create the text block
|
|
var tb = new TextBlock();
|
|
|
|
// Configure layout properties
|
|
tb.MaxWidth = 900;
|
|
tb.Alignment = TextAlignment.Center;
|
|
</code></pre>
|
|
<h2 id="creating-styles">Creating Styles</h2>
|
|
<p>Before you can add text to a text block, you'll need to create the styles
|
|
that will be applied to the text:</p>
|
|
<pre class="language-csharp"><code class="language-csharp">// Create normal style
|
|
var styleNormal = new Style()
|
|
{
|
|
FontFamily = "Arial",
|
|
FontSize = 14
|
|
}
|
|
|
|
// Create bold italic style
|
|
var styleBoldItalic = new Style()
|
|
{
|
|
FontFamily = "Arial",
|
|
FontSize = 14,
|
|
FontWeight = 700,
|
|
FontItalic = true,
|
|
}
|
|
</code></pre>
|
|
<h2 id="adding-text">Adding Text</h2>
|
|
<p>Now that you've created a text block and some styles, you can add text to the
|
|
text block with the <a href="./ref/Topten.RichTextKit.TextBlock.AddText">AddText()</a> method:</p>
|
|
<pre class="language-csharp"><code class="language-csharp">// Add text to the text block
|
|
tb.AddText("Hello World. ", styleNormal);
|
|
tb.AddText("Welcome to RichTextKit", styleBoldItalic)
|
|
</code></pre>
|
|
<p>That's it! You can now use the text block to <a href="rendering">render</a>, <a href="measuring">measure</a> and <a href="hittesting">hittest</a> its content.</p>
|
|
<h2 id="custom-istyle-implementation">Custom IStyle Implementation</h2>
|
|
<p>The above example uses the built in <a href="./ref/Topten.RichTextKit.Style">Style</a> class to define the styles to be
|
|
used. The Style class is a lightweight class and is a reasonable approach for
|
|
most scenarios.</p>
|
|
<p>You can however provide you own implementation of <a href="./ref/Topten.RichTextKit.IStyle">IStyle</a>. This
|
|
provides an easy way to plugin styling to a more comprehensive styling/DOM system should
|
|
you need it.</p>
|
|
<h2 id="re-using-textblocks">Re-using TextBlocks</h2>
|
|
<p>Text blocks are designed to be re-used. For example suppose you have a label control
|
|
that uses a text block to render its content, the recommended approach would be:</p>
|
|
<ol>
|
|
<li><p>Create and initialize a <a href="./ref/Topten.RichTextKit.TextBlock">TextBlock</a> instance with the text to be displayed.</p>
|
|
</li>
|
|
<li><p>Render that text block instance each time the control needs to be drawn.</p>
|
|
</li>
|
|
<li><p>When the label's text changes, instead of creating a new text block instance,
|
|
call the existing instance's <a href="./ref/Topten.RichTextKit.TextBlock.Clear">Clear()</a> method
|
|
and then add the updated text to the same instance.</p>
|
|
</li>
|
|
</ol>
|
|
<p>By re-using the same text block instance you can avoid pressure on the garbage collector
|
|
since the existing text block's internally allocated arrays can be re-used.</p>
|
|
<p>Another approach you might consider if you have many of pieces of text that rarely need
|
|
to be redrawn, would be to create a single TextBlock element and use the same instance
|
|
to draw each piece of text.</p>
|
|
|