与大写字母一样大的TextBlock(忽略字体升序/降序) [英] TextBlock as big as a capital letter (ignoring font ascender/descender)

查看:48
本文介绍了与大写字母一样大的TextBlock(忽略字体升序/降序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 TextBlock 上获得特定的行为,以使其高度仅包括大写字母的高度(从基线到顶部减去上升高度").请查看 Wikipedia 中的图像 Sphinx ,以了解我的意思.另外,下面的图片可能会更好地说明我的追求.

I am looking to get a specific behavior on TextBlock so that its height only includes the height of the capital letters (from baseline to top minus "ascender height"). Please see the image Sphinx from Wikipedia to see what I mean. Also the image below may indicate better what I am after.

我不是特别在寻找纯粹的XAML解决方案(可能是不可能的),因此后面(转换器)的C#代码也可以.

I am not specifically looking for a pure XAML solution (probably impossible) so a C# code behind (a converter) is also fine.

这是XamlPad中用于在上图中产生左侧A的XAML.

This is the XAML used in XamlPad to produce the left A in the image above.

<TextBlock Text="A" Background="Aquamarine" FontSize="120" HorizontalAlignment="Center" VerticalAlignment="Center" />

推荐答案

u可以尝试使用属性LineStackingStrategy ="BlockLineHeight"和LineHeight属性上的转换器,以及TextBlock高度上的转换器.这是转换器的示例代码

u can try to use attribute LineStackingStrategy="BlockLineHeight" and a Converter on the LineHeight attributes and a converter on the Height of TextBlock. This a sample code of converters

// 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();
    }
}

转换器上使用的系数取决于使用的族字体(基准线和行距):

The Coefficient used on converters depends on Used Family Fonts (Baseline and 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参数来计算Coeff.在此示例(Segeo UI)中,高度的系数= 0.715,线高= 0,875 *字体大小.

The best solution is to find how to calculate the Coeff based on parameters Baseline and LineSpacing of the FontFamily. In this sample (Segeo UI) the Coeff of Height = 0.715 and LineHeight = 0,875 * FontSize.

这篇关于与大写字母一样大的TextBlock(忽略字体升序/降序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆