Fonts in Silverlight

Tuesday, May 29th, 2007

Well I don’t know about you but I have yet work with a graphically rich development environment that makes embedding fonts easy. Saying that the examples always seem pretty easy which is true if you are adding a single font in a single field but when it boils down to multi language & multi font sites there is always going to be trouble. Enough of that for now.

In Silverlight embedding fonts is totally different to flash. In flash the font is compiled into the swf file. Since Silverlight 1.0 is js based there is nothing to compile it into and in 1.1 to my knowledge there is no way to extract resources from the dll if u stick the font in there (which u can do) u cant get it out again. What that means is you need to stick the font up on the server with the rest of the project resources. Then use the downloader object to download and enable the font in the site. The example below is for 1.1 should be easily adapted for 1.0.

public void someFunction()
{
//creates a new download object
Downloader downloader = new Downloader();

//calls the “downloader_Completed” function when it has completed downloading and the
//complete event is dispatched notice “+=” you don’t have that in flash will try get in it
//overloading in another post
downloader.Completed += new EventHandler(downloader_Completed);

//specify the location and method used to get the font
downloader.Open(“GET”, new Uri(“./Helvetica 35 Thin.ttf”, UriKind.Relative), true);

//starts downloading the font
downloader.Send();
}

void downloader_Completed(object sender, EventArgs e)
{

//gives the text field access to the font or font family that was in the downloader obj
someTextField.SetFontSource( (sender as Downloader));

//sets the font from the font family or the font
someTextField.FontFamily = “Helvetica 35 Thin”;

}

Leave a Reply

Your email address will not be published. Required fields are marked *