”;
In this chapter, we will look at what Silverlight offers to display text. The text block is used for all text rendering and Silverlight. Other important features are −
- It can be used to simple plain text or you can apply a mixture of formatting styles.
- Silverlight supports a standard set of built in fonts.
- You can also download custom fonts when your applications visual style need something less ordinary.
TextBlock
To display text we use Silverlight textbook element, which is a lightweight control for displaying small amounts of read-only text. In fact, we have already seen this quite a lot as its basic usage does not really need much explanation. You just set the text property and it displays that text for you.
<TextBlock Text = "Print Testing" HorizontalAlignment Center" FontFamily = "Georgia"/>
The hierarchical inheritance of TextBlock class is as follows,
Given below are the commonly used properties of TextBlock class.
Sr. No. | Property & Description |
---|---|
1 |
ContentEnd Gets a TextPointer object for the end of text content in the TextBlock. |
2 |
ContentStart Gets a TextPointer object for the start of text content in the TextBlock. |
3 |
IsTextSelectionEnabled Gets or sets a value that indicates whether text selection is enabled in the TextBlock, either through user action or calling selection-related API. |
4 |
IsTextSelectionEnabledProperty Identifies the IsTextSelectionEnabled dependency property. |
5 |
LineHeight Gets or sets the height of each line of content. |
6 |
MaxLines Gets or sets the maximum lines of text shown in the TextBlock. |
7 |
SelectedText Gets a text range of selected text. |
8 |
SelectionEnd Gets the end position of the text selected in the TextBlock. |
9 |
SelectionHighlightColor Gets or sets the brush used to highlight the selected text. |
10 |
SelectionStart Gets the starting position of the text selected in the TextBlock. |
11 |
Text Gets or sets the text contents of a TextBlock. |
12 |
TextAlignment Gets or sets a value that indicates the horizontal alignment of text content. |
13 |
TextTrimming Gets or sets the text trimming behavior to employ when content overflows the content area. |
14 |
TextWrapping Gets or sets how the TextBlock wraps text. |
Given below are commonly used events of TextBlock class.
Sr. No. | Event & Description |
---|---|
1 |
ContextMenuOpening Occurs when the system processes an interaction that displays a context menu. |
2 |
SelectionChanged Occurs when the text selection has changed. |
Given below are the commonly used methods in TextBlock class.
Sr. No. | Method & Description |
---|---|
1 |
Focus Focuses the TextBlock, as if it were a conventionally focusable control. |
2 |
Select Selects a range of text in the TextBlock. |
3 |
SelectAll Selects the entire contents in the TextBlock. |
Run
Sometimes you want fine-grained control over formatting and setting one style for an entire text block. It is sometimes useful to format individual words or even letters, and if you want this then instead of using the Text property, you put the text inside the TextBlock as content. If you are using a code, this corresponds to adding items to the TextBlock inline property.
Using this approach, you can add a series of run elements. Each Run supports the same font family, front weight, foreground and so on properties for controlling the text style. Although Run is a separate element this does not disrupt the flow.
Let us have a look at a simple example, which contains multiple Run element inside TextBlock. Given below is the XAML code.
<UserControl x:Class = "SilverlightRunDemo.MainPage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable = "d" d:DesignHeight = "300" d:DesignWidth = "400"> <Grid x:Name = "LayoutRoot" Background = "White"> <TextBlock Width = "192" TextWrapping = "Wrap" FontFamily = "Verdana"> <Run Text = "Hello, " /> <Run FontWeight = "Bold" Text = "world!" /> <Run Text = "You" /> <Run FontStyle = "Italic" Text = " are " /> <Run Text = "learning" FontSize = "40" FontFamily = "01d English Text MT" /> <Run Text = " the " /> <Run Text = "basics of " Foreground = "Blue" /> <Run Text = " Silverlight." FontSize = "30" /> </TextBlock> </Grid> </UserControl>
When the above code is compiled and executed, you will see the following output.
As you can see, this text block is arranged with different formatting styles by using the Run element.
By the way, you do not need to wrap every single bit of text in a run. You can leave most of the content of a text block as plain text and just apply run to the parts that need different formatting as shown below.
<TextBlock> Hello, <Run FontWeight = "Bold" Text =" world!"/> </TextBlock>
LineBreak
Silverlight usually ignores line breaks in the XAML. It assumes that most white spaces are there to make them easier to read because you actually want that space to appear.
Let us have a look at this XAML code, which has three separate lines of text in it.
<TextBlock> This is not the end. It is not even the beginning of the end. But it is, perhaps, the end of the beginning </TextBlock>
When the above code is compiled and executed, you will see the following output.
As you can see that it has ignored the line breaks and executed all the text together.
-
If you enable text wrapping, it will put line breaks in where it needs to be to make the text fit but it will ignore the line breaks in your example.
-
If you just want to add explicit line breaks, you need to add a line break tag inside your text block. The text follows it will start on a new line.
Let us have a look at the same example again by adding the LineBreak tag.
<TextBlock FontSize = "16"> This is not the end. <LineBreak/> It is not even the beginning of the end. <LineBreak/> But it is, perhaps, the end of the beginning </TextBlock>
When the above code is executed, you will see the that it now looks like as specified in XAML.
Built-in Fonts
Silverlight has a fixed set of built-in font families. The fonts actually have different family names for historical reasons. The default family is technically different on Mac OS and windows such as on Mac OS it is Lucida Grande, while on Windows it is the almost identical but named Lucida Sans Unicode.
Some of the most commonly used fonts are given below.
Fonts |
---|
Arial |
Arial Black |
Comic Sans MS |
Courier New |
Georgia |
Lucida Grande (Mac) or Lucida Sans Unicode (Windows) |
Times New Roman |
Trebuchet MS |
Verdana |
”;