Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Silverlight » Using Embedded Fonts in Silverlight 2

Using Embedded Fonts in Silverlight 2

This articles guides you on how to use other fonts in your application other than the ones provided by default in Silverlight 2.

Total page views :  1786
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor


MindCracker Survey


 
Today, I am going to show you guys, "How to you Embedded Fonts" in your Silverlight2 applications.

Silverlight2 has a very rich library for creating RIA (Rich Internet Applications) but has less features and functionalities compared to its father WPF(Windows Presentation Foundation). This is so because, Silverlight is a browser plugin which gets downloaded to the client machine, the first time a user access any web page having Silverlight content. So for making the download experience smooth, the Silverlight plugin/player has been packed with the minimum and most required features and functionalities, so that its size is less, and it can be downloaded to the client computer ASAP. Once, the plugin has been installed, the user can access the Silverlight pages without any interruption. Currently the Silverlight2 plugin has a download size of 4.6MB.

This is the reason why, Silverlight2 is not provided with a wide variety of Fonts. By Default, a Silverlight2 application supports and provides 9 fonts, mentioned below:


fontNames.bmp


So for using Fonts other than than the ones specified above, we would need to embed it into our application assembly as a "Resource".

We can use the embedded font either using "Code" or "Markup".

For either of the way, first we need to add the desired font into our Silverlight2 application project. I am using a freely available font named "A Cut Above the Rest". You can easily find it and several other free fonts on the following website -
http://www.urbanfonts.com/free-fonts.htm
 

To add the font file to your project, perform the below steps:

  1. Right Click your Silverlight application project, and choose "Add", then "Add Existing Item" menu Item. Then select the font file(with .TTF extension) to be used.


    AddFont.bmp

  2. After the font file gets added to your project, Right-click on the file and click Properties. On the properties window, set the "Build Action" property to "Resource". Setting this property will embed your newly added font file to your application assembly as a binary resource, which can later be used for the usage.

    BuildAction.bmp
So, now we are done and ready to use our newly added font "A Cut Above the Rest". Below example uses XAML Markup to set the new font "A Cut above the Rest" to a TextBlock element.

<TextBlock x:Name="MyLabel" Foreground="Blue" FontFamily="ACUTATR_.TTF#A Cut Above the Rest" FontSize="25">

The above example has a XAML Markup having the following syntax in the "FontFamily" property:  "FontFileName#FontName". For our font, the file name is "ACUTATR_.TTF" and the font name is - "A Cut Above the Rest".

Now, the second way to use embedded font in Silverlight2 application is to use Code. As I work in C#, so I am providing a C# example:
 

using System.Windows.Resources;

/*Use Embedded Font - A Cut Above the Rest*/
StreamResourceInfo sri = Application.GetResourceStream(new Uri("PracticeEmbeddedFonts;component/ACUTATR_.TTF", UriKind.Relative));
MyLabel.FontSource = new FontSource(sri.Stream);
MyLabel.FontFamily = new FontFamily("A Cut Above the Rest");
 

Here, 3 classes are used to retrieve the font from the Font file.
  1. StreamResourceInfo
  2. Application
  3. FontSource

In the above code, the font is retrieved from the assembly as a Resource by using the "Application.GetResourceStream()" method. It accepts 2 arguments. The first argument here follow the following format :- "AssemblyName:component/FontFileName.TTF"

After retrieving the font from the assembly, we can assign it to the control we want to, by first assigning the "FontSource" property with our Font Resource stream object's Stream property. Then we assign the name of the font from that font file i.e., "A Cut Above the Rest".

For the above code to work, an important assembly namespace reference will need to be made i.e., - "using System.Windows.Resources;

After successfully coding the application, our TextBlock will have a font like shown in the below picture:


SampleText.bmp

NOTE :- Not all the fonts that are on the Internet are available for free distribution. For many of them you will need to purchase a license from the owner. So please take this note in mind, while using any font.


Login to add your contents and source code to this article
 Article Extensions
Contents added by Deepak Pathak on Jun 15, 2009
Download File: UseEmbeddedFonts.zip
 About the author
 
Deepak Pathak
Currently working in Espire Infolabs Pvt. Ltd., New Delhi as a Snr. Software Engineer. I m currently working on ASP.NET, XML Web Serivces and C#. I love Microsoft Techonolgies and currently have a keen interest in SOA and its current applications through WCF, Silverlight. I am currently learning LINQ, jQuery and ADO.NET Entity Framework.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2010  Mindcracker LLC. All Rights Reserved