I have a True type font (.ttf) file hosted on a service. I obtain it in the form of a byte array. I need to set the font in that file during run-time.
I am facing the problem on Windows Phone 7 and the service used is a simple WCF service which provides the .ttf file as a byte-array.
This is what I have done till now but it doesn’t seem to work.. :
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
string fontPath = string.Empty;
string ss = string.Empty;
IsolatedStorageFileStream fs = new IsolatedStorageFileStream("foo.ttf", System.IO.FileMode.Create, file);
fs.Write(e.Result.byteArray, 0, e.Result.byteArray.Length);
fs.Dispose();
fs = new IsolatedStorageFileStream("foo.ttf", FileMode.Open, file);
ss = fs.Name + @"#My Font";
textBlock1.FontFamily = new System.Windows.Media.FontFamily(ss);
I finally figured it out. There is a FontSource property of a textblock which has a constructor which takes a STREAM as parameter. The .ttf file can be read from the Isolated Storage in the form of a stream.
The FontFamily can then be set as any of the fonts present in the .ttf font file.
The code snippet is as follows: