Using SetFontSource for Downloaded Fonts
The SetFontSource method can be used as a way of adding downloaded font content to the existing collection of type faces for a TextBlock object. The downloader parameter identifies the Downloader object representing the downloaded content. The downloaded content can either be an individual font file or packaged content, such as a Zip file, that contains one or more font files.
Note: Font files used with the SetFontSource method must be OpenType or TrueType® fonts, and have the file extension, "TTF".
The following JavaScript example shows how to use the Downloader object to download an individual font file.
JavaScript |
---|
// Event handler for initializing and executing a font file download request. function onMouseLeftButtonUp(sender, eventArgs) { // Retrieve a reference to the control. var control = sender.getHost(); // Create a Downloader object. var downloader = control.createObject("downloader"); // Add Completed event. downloader.addEventListener("Completed", "onCompleted"); // Initialize the Downloader request. downloader.open("GET", "SHOWG.TTF", true); // Execute the Downloader request. downloader.send(); } |
When the font file has been downloaded, it needs to be added to the TextBlock's collection of type faces. Once it has been added to the collection, it can then be selected using the FontFamily property. The following JavaScript example shows how to use the SetFontSource method to add the the font file to the type face collection, and then set the FontFamily property to display the TextBlock with the downloaded font:
JavaScript |
---|
// Event handler for the Completed event. function onCompleted(sender, eventArgs) { // Retrieve the TextBlock object. var myTextBlock = sender.findName("myTextBlock"); // Add the font files in the downloaded object to the TextBlock's type face collection. myTextBlock.setFontSource(sender); // Set the FontFamily property to the friendly name of the font. myTextBlock.fontFamily = "Showcard Gothic"; myTextBlock.text = "Showcard Gothic"; } |
You can also download font files contained in a packaged format, such as a Zip file. The Zip file can contain other content files, such as image files.
其实我们知道silverlight不支持中文就是因为没有相应的字体文件,通过下载我们提供的字体文件,我们可以让silverlight显示中文。网上有很多介绍这个的实现方式。
