我希望在TextBlock上获得一个特定的行为,以便它的高度只包括大写字母的高度(从基线到顶部减去“递增高度”)。请参阅来自Wikipedia的图像Sphinx,以了解我的意思。此外,下面的图像可能会更好地表明我在寻找什么。


我并不是专门寻找一个纯粹的XAML解决方案(可能是不可能的),所以一个C#代码(一个转换器)也是可以的。
这是在XamlPad中用来生成上图中左A的XAML。
<TextBlock Text="A" Background="Aquamarine" FontSize="120" HorizontalAlignment="Center" VerticalAlignment="Center" />发布于 2013-04-08 18:09:34
你可以尝试使用属性LineStackingStrategy="BlockLineHeight“和LineHeight属性上的转换器,以及TextBlock高度上的转换器。这是转换器的示例代码
// Height Converter
public class FontSizeToHeightConverter : IValueConverter
{
public static double COEFF = 0.715;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (double)value * COEFF;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
// LineHeightConverter
public class FontSizeToLineHeightConverter : IValueConverter
{
public static double COEFF = 0.875;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return double.Parse(value.ToString()) * COEFF;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}转换器上使用的系数取决于所使用的系列字体(Baseline和LineSpacing):
<TextBlock Text="ABC" Background="Aqua" LineStackingStrategy="BlockLineHeight"
FontSize="{Binding ElementName=textBox1, Path=Text}"
FontFamily="{Binding ElementName=listFonts, Path=SelectedItem}"
Height="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Mode=OneWay, Converter={StaticResource FontSizeToHeightConverter1}}"
LineHeight="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Converter={StaticResource FontSizeToLineHeightConverter}}"/>

最好的解决方案是找到如何根据FontFamily的参数Baseline和LineSpacing来计算系数。在此示例(Segeo UI)中,Height的系数= 0.715,LineHeight = 0,875 * FontSize。
发布于 2012-03-21 00:50:01
更新:
如果我没理解错的话,我知道一些诀窍,
你可以用RenderTransform来Scale它,这通常是最有效的方法;
<TextBlock Text="Blah">
<TextBlock.RenderTransform>
<CompositeTransform ScaleY="3"/>
</TextBlock.RenderTransform>
</TextBlock>或者,您可以将TextBlock嵌入到Viewbox中,以“缩放”文本以适应其容器的边界,例如,如果您在网格行上设置了硬高度值,如;
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<Viewbox VerticalAlignment="Stretch" Height="Auto">
<!-- The textblock and its contents are
stretched to fill its parent -->
<TextBlock Text="Sphinx" />
</Viewbox>
<Viewbox Grid.Row="2" VerticalAlignment="Stretch" Height="Auto">
<!-- The textblock and its contents are
stretched to fill its parent -->
<TextBlock Text="Sphinx2" />
</Viewbox>或者,您可以将FontSize绑定到容器元素,如;
<Grid x:Name="MyText" Height="120">
<TextBlock FontSize="{Binding ElementName=MyText, Path=Height}" Text="Sphinx" />
</Grid>它们可能会呈现出你想要的效果?
https://stackoverflow.com/questions/9785322
复制相似问题