The TextBlock object is the primary element for displaying text in Silverlight-based applications. The following XAML example shows how to define a TextBlock element and set its Text property to a character string.
<Canvas>
<TextBlock Text="Hello, world!" />
</Canvas>
The following illustration displays the result of the previous XAML content example.
TextBlock rendering with default font properties
.png)
There are several interesting items to note about the rendered text:
The default value of the FontSize property of the rendered TextBlock is 14.666 pixels, which is exactly 11 points.
The default value of the FontFamily property of the rendered TextBlock is "Portable User Interface". This value represents a composite font that uses several fonts to implement the range of international languages supported by Silverlight. This includes "Lucida Sans Unicode" and "Lucida Grande" for many Western writing systems, and many more for East Asian writing systems.
No font files are required on the hosting Web server, or residing in the same directory as the XAML content, to enable the default font.
When setting text in a TextBlock, it is not necessary to specify the Text property explicitly in XAML. The following XAML example shows how to specify text that is enclosed in a TextBlock element.
<Canvas>
<TextBlock>Hello, world!</TextBlock>
</Canvas>
Note: |
|---|
Leading or trailing white space is not preserved when setting the
Text property.
|
Specifying Font Attributes
When using text elements, such as a TextBlock, you can specify various font attributes. The following table lists the font attributes that you can specify for text elements.
Property
|
Description
|
Default Value
|
|---|
FontFamily |
Specifies the desired font family, such as "Times New Roman".
|
The default value is "Portable User Interface", which is described in the "Text Elements" section earlier in this topic.
|
FontSize |
Specifies the desired font size in pixels. The values must be non-negative.
|
The default value is 14.666 pixels, which is exactly 11 points.
|
FontStretch |
Specifies the desired glyph width of the font. This property can only be used to specify fonts that already exist in a font family. It does not cause programmatic stretching of glyphs.
|
The default value is Normal.
|
FontStyle |
Specifies whether the desired font style is normal or italic.
|
The default value is Normal.
|
FontWeight |
Specifies the desired glyph weight of the font. This property can only be used to specify fonts that already exist in a font family. It does not programmatically create alternative weights, except when a family contains a normal weight font, but not a bold weight font. In this case, Silverlight will simulate a bold weight font by increasing the width of strokes using a 2D graphic algorithm.
|
The default value is Normal.
|
The following XAML example shows how to define a TextBlock element and specify all of the font attributes listed in the previous table.
Note: |
|---|
Depending on the font that
Silverlight selects, the specified font attributes may not be the ones that are used during rendering. For more information, see the "Font Selection" section later in this topic.
|
<TextBlock
Text="Font Attributes"
FontFamily="Verdana"
FontSize="36"
FontStretch="UltraExpanded"
FontStyle="Italic"
FontWeight="ExtraBlack" />
The following illustration displays the result of the previous XAML content example.
TextBlock rendering with defined font property values
.png)
Supported Local Fonts
Text elements in Silverlight can use a subset of the fonts on the client computer. The following illustration shows the Latin fonts that Silverlight text elements can use from the local computer.
Supported local Latin fonts for text elements
.png)
Additionally, text elements can use the Arial Unicode MS and Cambria Math fonts from the local computer. Lucida Grande and Lucida Sans Unicode are aliases for the same font and are specified as a pair for compatibility reasons. Portable User Interface is a composite font, described earlier in the "Text Elements" section.
If you want to work with East Asian text, Silverlight text elements can use the following East Asian fonts from the local computer.
Batang
BatangChe
DFKai-SB
Dotum
DutumChe
FangSong
GulimChe
Gungsuh
GungsuhChe
KaiTi
Malgun Gothic
Meiryo
Microsoft JhengHei
Microsoft YaHei
MingLiU
MingLiu_HKSCS
MingLiu_HKSCS-ExtB
MingLiu-ExtB
MS Gothic
MS Mincho
MS PGothic
MS PMincho
MS UI Gothic
NSimSun
NSimSun-18030
PMingLiU
PMingLiu-ExtB
SimHei
SimSun
SimSun-18030
SimSun-ExtB
Downloading Fonts
Silverlight does not include any fonts in its installation package. If you want to use a font for a TextBlock or TextBox element that is not on the list of supported local fonts, you can specify the font in XAML by using the FontFamily property, or in code using the FontSource property. The FontFamily property can specify a single font file or a zip of font files, but they must be included in the .xap file. When using FontSource,your code must load the font (or zip of fonts) from a stream such as isolated storage.
Font Selection
The font that you specify, may not necessarily be the font that Silverlight uses. For the TextBlock element, Silverlight chooses the font from the supported local fonts, from the fonts provided in a packaged file referenced in the FontFamily property, or from the file passed to the FontSource property. The font that is selected is the best match based on the values of the FontFamily, FontStretch, FontStyle, and FontWeight properties. It is important to specify all four of these font properties to make sure that an accurate and consistent font selection is made for your TextBlock. The TextBox and Run objects contain similar properties that control their font selection.
Using the Run and LineBreak Objects
The Run object is a text element that represents a discrete section of formatted or unformatted text. The LineBreak object represents an explicit new line in a TextBlock. The following XAML example shows how to define several differently formatted text strings in a TextBlock by using Run objects.
<!-- Display formatted text as Run objects within a TextBlock. -->
<Canvas>
<TextBlock
FontFamily="Arial" Width="400" Text="Sample text formatting runs">
<LineBreak/>
<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
<LineBreak/>
<Run Foreground="Teal" FontFamily="Times New Roman" FontSize="18" FontStyle="Italic">Times New Roman Italic 18</Run>
<LineBreak/>
<Run Foreground="SteelBlue" FontFamily="Verdana" FontSize="14" FontWeight="Bold">Verdana Bold 14</Run>
</TextBlock>
</Canvas>
The following illustration shows the rendered formatted text from the previous XAML content example.
TextBlock rendering multiple Run objects
.png)
Notice that the LineBreak object forces the text in each Run to display on a separate line. Without the LineBreak object, the text in each Run would flow together as one line and would eventually be truncated after exceeding the TextBlock object's Width value. The following illustration shows how the formatted text would render without using LineBreak objects.
TextBlock rendering multiple Run objects without LineBreak objects
.png)