Multiple Dynamic Font loading
Hi,
I wanted to load multiple fonts dynamically on a single page. I have been able to load single font, but trying to load multiple fonts results in falling back to default font. Does anyone know how to do that.
I need this as I wrote a multi lingual application whose entry point should display text for all the languages I am supporting.
Ofcourse I can achieve this by making images, but text would be convenient.
Re: Multiple Dynamic Font loading
if these are fonts that you package with your app you need to:
- Add the font to a folder on your app
- Change the properties of the font file to: Build Action: Content; Copy to output directory: Copy if newer
Then to use the font on your project, in xaml:
[QUOTE]<TextBlock Text="Sample text with a different font" FontFamily="fonts/nameofthefont.ttf#nameofthefont" FontSize="60"/>[/QUOTE]
The name after the # is the actual Font name.
Re: Multiple Dynamic Font loading
Hi Joao,
Thanks this approach works.
May be I should have phrased the question in a better way earlier. I think, I had done too many changes to code and saw some other problem.
But Here is my attempt two with more focused problem :)
I would like to load the font dynamically rather than hard code into the xaml.
For eg:
I am loading font for whole page like this ...
[I]PageTitle.FontFamily = new FontFamily("<new font name>"); PageTitle.Text = "<some text in the new font>"[/I]
This works.
Similarly I would like to do the same for textblock.
TextBlk.FontFamily = new FontFamily("<new font name>"); TextBlk.Text = "<some text in the new font>"
But the compiler throws error saying [I]"System.Windows.Controls.Control.FontFamily is a property but is used like a type."[/I]
Any idea what is wrong here?
Why I need this - I can add/remove languagues and text blocks on the fly, rather than compile the code everytime.
Just pick languages from server and push them down with some logic ofcourse :)
Regards
Mrudul
Re: Multiple Dynamic Font loading
I just compiled a project with the same line of code as this one, without any problem.
[CODE]TextBlk.FontFamily = new FontFamily("<new font name>"); TextBlk.Text = "<some text in the new font>";[/CODE]
Do you use the normal TextBlock Control ? or have you made a custom one ?
Re: Multiple Dynamic Font loading
Perhaps the namespace (System.Windows.Media) is not used in the scope for the problematic code.
Re: Multiple Dynamic Font loading
Hmm,
Yep, System.Windows.Media; was indeed a problem. Strange I donot remember adding the same namespace for other page.
Anyways... thanks for the solution.
Re: Multiple Dynamic Font loading
It is a wild guess, but it may be a copy-paste thing.
Version 1: first time you were typing the code, and probably using code completion. In that case the tool may automatically add imports (I still have not started coding WP, but this is what is happening in many Eclipse-based environments for example). However when you grab the code and paste it into an other file, this probably does not happen again
Version 2: copying code from somewhere (an example, a Web page, etc.), but copying it partially to the second place