WP7 - Not picking up embedded fonts

2019-07-23 22:57发布

问题:

I have been trying to use some custom fonts within my WP7 app.

I followed the example as per http://www.jeffblankenburg.com/2010/10/24/31-days-of-windows-phone-day-24-embedding-fonts/ but in my project it simply will not pick up the custom font. I have downloaded the sample project in this example and this one does work okay.

So, I have now used the same font used in there, and included in my project. I have ensured that the properties on the file are identical (Build Action = 'Content' and Copy To Output Directory = "Always")

I have used in my XAML in exactly the same way i.e.

  FontFamily="Fonts/DigitalDream.ttf#Digital Dream" FontSize="24"

But to no avail. The custom font is just ignored. Is there something that I am missing, or a setting .. anything that would allow this to work in one project, but not another?

I have discovered that it will work if my XAML page is defined at the root level, i.e. immediately under the project, but in my case I have it in a 'Views' folder.

My understanding was that FontFamily="/Fonts/DigitalDream.ttf#Digital Dream" FontSize="24"

should start at the root folder of the project?

  • thanks

回答1:

Custom fonts can be a little tricky. I spent several hours once trying to embed an .otf font only to find that it just didn't seem to be doable.

You're on the right track, though. A .ttf font should be embedable. It's hard to get the right combination of paths and filenames to make the font work, though.

The easiest way I've found to handle some of these more difficult "magic string" type of situations in XAML is to let Expression Blend do the heavy lifting.

Here's a blog post about using Blend to embed fonts. The nice thing about using Blend is that it gives you some visual indications that things are set up correctly--i.e. you can see the font you want in the font selection menu. This is what finally clued me in on the .otf issue. I couldn't see the font in Blend.

And here's an example of a custom font that I've used in one of my apps:

<TextBlock Name="MyTextblock"
   FontFamily="/MyAppName;component/fonts/Fonts.zip#Segoe UI Mono">

That's FontFamily URI is a pain to get right. I'm pretty sure that's your problem.



回答2:

I was actually having the same problem. I followed everything to the dot and still I wasn't able to embed one particular font. So I created a new project and tried embedding the font there. Still no luck. I then tried embedding some other font. And lo and behold, as soon as I typed the FontFamily attribute, I could see that it had embedded. This was particularly odd since I didn't have to change any properties of the font.

I opened the project in Blend and clicked on the font property box to the right. It took a few seconds and then it embedded the font I was having problems with in the first place.