Why FontStretch does not work in WPF?

冷暖自知 提交于 2019-11-29 11:37:48

问题


I am trying setting FontStretch property on a TextBlock in WPF but it seems that it does not work. I tried Expanded, Condensed, etc. but the text appearance does not change.

I am working on Windows XP with Framework 4.0 and tested both with Verdana and Arial.

Does it work only on Windows 7 or only with some specific fonts?

EDIT: If it does not work with all fonts, is there a list of fonts that support this feature? Or is it possible to modify a font like Verdana/Arial to support it?


回答1:


To get a similar effect to FontStretch in a font that doesn't support it, you can use a LayoutTransform on the TextBlock:

<Application.Resources>
  <ScaleTransform x:Key="FontStretchCondensed" ScaleX="0.8" />
  <ScaleTransform x:Key="FontStretchExpanded" ScaleX="1.2" />
</Application.Resources>

...

<TextBlock Text="This is my text"
           LayoutTransform="{StaticResource FontStretchCondensed}" />

This can also be set in a style if you want to have all text in TextBlocks appear condensed:

<Style TargetType="TextBlock">
  <Style.Setters>
    <Setter Property="LayoutTransform" Value="{StaticResource FontStretchCondensed}" />
  </Style.Setters>
</Style>



回答2:


Unlike font properties such as bold or italic, WPF does not simulate font properties such as stretch or small caps. The font used must support this itself.

To see which fonts support FontStretch, you'll need to look for fonts that have a separate font file for e.g. Condensed or Expanded. And then only properly linked TrueType/OpenType fonts will work. Some font vendors do not properly link stretched or condensed variants of their fonts to the regular version, so WPF has no idea that these font variants are related.




回答3:


By measuring some text (TextBlock.Measure), I find that Arial Narrow is × 0.82 the width of Arial.

So textBlock.LayoutTransform = new System.Windows.Media.ScaleTransform(0.82, 1.0); when applied to Arial might approximate Arial Narrow.




回答4:


I believe FontStretch doesn't work for all fonts.




回答5:


It only works for OpenType fonts. For more information:

http://en.wikipedia.org/wiki/OpenType

http://www.adobe.com/type/opentype/

http://www.microsoft.com/typography/WhatIsOpenType.mspx



来源:https://stackoverflow.com/questions/2948290/why-fontstretch-does-not-work-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!