Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #45: Text Formatting with the TextBlock control

Silverlight comes with a very useful control called TextBlock. This control allows you to render read only text.  Displaying text via this control is as easy as:

<TextBlock Text="Hello There"></TextBlock>

 image

There are also a variety of options you can apply to your font. These include:

1. FontFamily. The font typeface family name.

<TextBlock FontFamily="Courier New">Hello There</TextBlock>
<TextBlock FontFamily="Times New Roman" Canvas.Top="10">Hello There</TextBlock>
<TextBlock FontFamily="Verdana" Canvas.Top="20">Hello There</TextBlock>

image

2. FontSize. The font size in pixels.

<TextBlock FontSize="10">Hello There</TextBlock>
<TextBlock FontSize="20" Canvas.Top="20">Hello There</TextBlock>
<TextBlock FontSize="30" Canvas.Top="50">Hello There</TextBlock>

 image

2 FontStetch. Options include:

Font stretch

usWidthClass

% of normal

UltraCondensed

1

50.0%

ExtraCondensed

2

62.5%

Condensed

3

75.0%

SemiCondensed

4

87.5%

Normal

5

100%

SemiExpanded

6

112.5%

Expanded

7

125.0%

ExtraExpanded

8

150.0%

UltraExpanded

9

200.0%

Note that the support for the above options will depend upon the font you are using.

3 FontWeight. Options include: Thin, ExtraLight, Light, Normal, Medium, SemiBold, Bold, ExtraBold, Black, ExtraBlack.

Note that the support for the above options will depend upon the font you are using.

<TextBlock Text="Hello There" FontWeight="Normal"  Canvas.Top="0"></TextBlock>

<TextBlock Text="Hello There" FontWeight="Bold"  Canvas.Top="10"></TextBlock>

image

4 FontStyle: Options include: Normal, Italic

<TextBlock Text="Hello There" FontStyle="Normal" Canvas.Top="0"></TextBlock>

<TextBlock Text="Hello There" FontStyle="Italic"  Canvas.Top="10"></TextBlock>

image

5. Foreground. This allows you to change the color of the font. You can use brushes including solid color, gradient, image or video brushes to create a wide variety of effects.

<TextBlock Text="Hello There" FontSize="20" FontFamily="Arial" Canvas.Top="0">  <TextBlock.Foreground>
    <LinearGradientBrush>
        <GradientStop Color="#FF0000FF" Offset="0.0" />
        <GradientStop Color="#FFEEEEEE" Offset="1.0" />
      </LinearGradientBrush>
    </TextBlock.Foreground>
</TextBlock>

<TextBlock Text="Hello There" FontSize="20" FontFamily="Arial" Canvas.Top="20" Foreground="Red"></TextBlock>

image 

6. TextDecorations. A text decoration is a visual ornament that you can add to text. Currently the only supported decoration is underline.

<TextBlock TextDecorations="underline">Hi there</TextBlock>

image

7. Runs. Last, you can inter-mix different runs of text formatting within the same block using the Run element. For example, you can display a string like this:

<TextBlock>
    <Run FontWeight="Bold">Hello There.</Run>
    <Run Foreground="Red">How are you?</Run>
    <Run FontStyle="Italic">I am fine thanks!</Run>
    <Run>漢字</Run>
</TextBlock>

image

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Silverlight comes with a very useful control called TextBlock . This control allows you to render read

# September 24, 2008 6:41 PM

sariel said:

Hi Mike just an FYI I have vista 64 IE 7.0.6 and Mcafee virus.  For some reason I am getting told in the IE address bar that this could be a phising site.  I am sorry that I dont have more information but I have actually never seen this message before this.  It is a IE message as the more information thing links to a Microsoft site

phishingfilter.microsoft.com/feedback.aspx

I have submitted feedback saying that this is NOT a phising site so lets see how that goes.  

# September 24, 2008 7:14 PM

2008 September 25 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 25 - Links for today &laquo; My (almost) Daily Links

# September 25, 2008 12:48 AM

Mike Snows Tipp des Tages #43, #44 und #45 at Blog von J??rgen Ebner said:

Pingback from  Mike Snows Tipp des Tages #43, #44 und #45 at Blog von J??rgen Ebner

# September 25, 2008 3:24 AM

Dew Drop - September 25, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - September 25, 2008 | Alvin Ashcraft's Morning Dew

# September 25, 2008 7:50 AM

Silverlight news for September 25, 2008 said:

Pingback from  Silverlight news for September 25, 2008

# September 25, 2008 10:58 AM

mike.snow said:

Sariel- Thanks for the report. I filled out the form to stating its not a phishing site.

# September 25, 2008 11:11 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

Default Fonts By Default, Silverlight supports the following local fonts (as shown rendered in Silverlight

# September 25, 2008 1:52 PM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #46 Title: Font Support in Silverlight Silverlight Tip of the Day #45: Title:

# September 25, 2008 1:55 PM

Community Blogs said:

Tim Heuer on Isolated Storage, Dan Wahlin Animating Clipping Paths, Mike Snow on Fonts and TextBlocks

# September 25, 2008 6:45 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

TextBlocks are a great control to use in Silverlight to display read only text. In Tip of the Day #45

# October 9, 2008 7:56 PM

unruledboy2 said:

isn't the underline or strikethrough are all font styles like other systems behave? why a separate property?

# October 10, 2008 11:02 PM

unruledboy2 said:

and you did not mention <LineBreak> :)

# October 10, 2008 11:08 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:26 AM