我希望在TextBlock上获得特定的行为,使其高度仅包括大写字母的高度(从基线到顶部减去“上升高度”).请从
Wikipedia看到Sphinx图片,看看我的意思.此外,下面的图片可能更好地表明我所追求的.
@H_502_9@解决方法
我不是专门寻找纯XAML解决方案(可能是不可能的)所以后面的C#代码(转换器)也没问题.
这是XamlPad中用于在上图中生成左A的XAML.
<TextBlock Text="A" Background="Aquamarine" FontSize="120" HorizontalAlignment="Center" VerticalAlignment="Center" />
你可以尝试在LineHeight属性上使用属性LineStackingStrategy =“BlockLineHeight”和转换器,在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,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // LineHeightConverter public class FontSizeToLineHeightConverter : IValueConverter { public static double COEFF = 0.875; public object Convert(object value,System.Globalization.CultureInfo culture) { return double.Parse(value.ToString()) * COEFF; } public object ConvertBack(object value,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
转换器上使用的系数取决于使用的族字体(基线和行间距):
<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},Converter={StaticResource FontSizeToLineHeightConverter}}"/>
最好的解决方案是找到如何根据FontFamily的参数Baseline和LineSpacing计算Coeff.在此示例(Segeo UI)中,Coeff of Height = 0.715并且LineHeight = 0,875 * FontSize.