Silverlight – Text

Silverlight – Text

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,

textblock

Given below are the commonly used properties of TextBlock class.

Given below are commonly used events of TextBlock class.

Given below are the commonly used methods in TextBlock class.

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.

run_inside_textblock

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.

linebreak

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.

add_linebreak_tag

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

Was this article helpful?

Related Articles

Leave A Comment?