- Silverlight - Printing
- Silverlight - Video and Audio
- Silverlight - Animation
- Silverlight - Text
- Silverlight - Isolated Storage
- Silverlight - Input Handling
- Silverlight - View Model
- Silverlight - File Access
- Silverlight - Applications, Resources
- Silverlight - Out-of-Browser
- Silverlight - Browser Integration
- Silverlight - Data Binding
- Silverlight - Visual State
- Silverlight - Templates
- Silverlight - ListBox
- Silverlight - Content Model
- Silverlight - Buttons
- Silverlight - Controls
- Silverlight - CSS
- Constrained vs. Unconstrained
- Silverlight - Dynamic Layout
- Silverlight - Fixed Layouts
- Silverlight - Project Types
- Silverlight - XAML Overview
- Silverlight - Getting Started
- Silverlight - Environment Setup
- Silverlight - Overview
- Silverlight - Home
Silverlight Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Silverpght - Text
In this chapter, we will look at what Silverpght offers to display text. The text block is used for all text rendering and Silverpght. Other important features are −
It can be used to simple plain text or you can apply a mixture of formatting styles.
Silverpght supports a standard set of built in fonts.
You can also download custom fonts when your apppcations visual style need something less ordinary.
TextBlock
To display text we use Silverpght textbook element, which is a pghtweight 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" HorizontalApgnment 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 calpng selection-related API. |
4 | IsTextSelectionEnabledProperty Identifies the IsTextSelectionEnabled dependency property. |
5 | LineHeight Gets or sets the height of each pne of content. |
6 | MaxLines Gets or sets the maximum pnes 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 | SelectionHighpghtColor Gets or sets the brush used to highpght 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 | TextApgnment Gets or sets a value that indicates the horizontal apgnment 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 inspanidual 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 inpne 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 controlpng 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 = "SilverpghtRunDemo.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-compatibipty/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 = "Itapc" Text = " are " /> <Run Text = "learning" FontSize = "40" FontFamily = "01d Engpsh Text MT" /> <Run Text = " the " /> <Run Text = "basics of " Foreground = "Blue" /> <Run Text = " Silverpght." 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
Silverpght usually ignores pne 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 pnes 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 pne breaks and executed all the text together.
If you enable text wrapping, it will put pne breaks in where it needs to be to make the text fit but it will ignore the pne breaks in your example.
If you just want to add exppcit pne breaks, you need to add a pne break tag inside your text block. The text follows it will start on a new pne.
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 pke as specified in XAML.
Built-in Fonts
Silverpght has a fixed set of built-in font famipes. 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 |