Adds font files in the downloaded content to the object's collection of
type faces.
| XAML | Cannot use methods in XAML. |
| Scripting | object.SetFontSource(downloader)
|
Parameters
| downloader | Downloader The object containing the downloaded
content. Set downloader to null to restore the default font for
the object. |
Examples
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 plug-in.
var plugin = sender.getHost();
// Create a Downloader object.
var downloader = plugin.createObject("downloader");
// Add Completed event.
downloader.addEventListener("Completed", onCompleted);
// Initialize the Downloader request.
downloader.open("GET", "SHOWG.TTF");
// Execute the Downloader request.
// NOTE: downloader APIs disallow file:\\ scheme
// you must run this sample over localhost: or off a server or the following call will fail
downloader.send();
}
|
When the font file has been downloaded, it needs to be added to the
TextBlock's collection of type faces, which is the purpose of SetFontSource. 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, and those files are ignored for purposes of what gets added to the font collection.
Images in a ZIP file
The following JavaScript example shows how to use the Downloader object to download a Zip file containing an image file and multiple font files.
| JavaScript |
// Initialize the Downloader request. Zip file contains: Coco.png, Britanic.ttf, Erasbd.ttf, Showg.ttf
downloader.open("GET", "myMediaAssets.zip");
|
When the Zip file has been downloaded, the font files it contains must be added to the TextBlock's collection of type faces. Once the font files have been added to the collection, they can then be selected using the FontFamily property. The following
JavaScript example shows how to use the SetSource and SetFontSource methods to
use the downloaded content.
| JavaScript |
// Event handler for the Completed event.
function onCompleted(sender, eventArgs)
{
// Retrieve the Image object.
var myImage = sender.findName("myImage");
// Set the Source property of the Image object to the specific image
// within the downloaded Zip package file.
myImage.setSource(sender, "Coco.png");
// Retrieve the TextBlock object.
var myTextBlock = sender.findName("myTextBlock");
// Add the font files in the downloaded package 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";
}
|
The following JavaScript example shows how to define a Silverlight keyboard event
by using the AddEventListener method:
| JavaScript |
function onLoaded(sender, eventArgs)
{
// Set the root Canvas object to a KeyDown event handler function.
var token = sender.addEventListener("keyDown", onKeyDown);
}
function onKeyDown(sender, keyEventArgs)
{
// Determine whether the keystroke combination CTRL+V was detected.
if ((keyEventArgs.key == 51) && (keyEventArgs.ctrl == true))
{
// Retrieve a reference to the plug-in.
var slPlugin = sender.getHost();
// Display the current version of the plug-in.
alert("Silverlight version: " + slPlugin.isVersionSupported("1.0"));
}
}
|
To return to the default font used to display the TextBlock, set the downloader parameter of the SetFontSource method to null.
The following JavaScript example shows how to use the SetFontSource method with a null parameter value.
| JavaScript |
// Retrieve the TextBlock object.
var myTextBlock = sender.findName("myTextBlock");
// Remove the custom font setting.
myTextBlock.setFontSource(null);
|
Applies To
TextBlock
See Also
Using a Downloader Object
Downloader