c# – 与大写字母一样大的TextBlock(忽略字体ascender / descend
发布时间:2020-12-15 07:53:54 所属栏目:百科 来源:网络整理
导读:我希望在TextBlock上获得特定的行为,使其高度仅包括大写字母的高度(从基线到顶部减去“上升高度”).请从 Wikipedia看到Sphinx图片,看看我的意思.此外,下面的图片可能更好地表明我所追求的. 我不是专门寻找纯XAML解决方案(可能是不可能的)所以后面的C#代码(转
我希望在TextBlock上获得特定的行为,使其高度仅包括大写字母的高度(从基线到顶部减去“上升高度”).请从
Wikipedia看到Sphinx图片,看看我的意思.此外,下面的图片可能更好地表明我所追求的.
我不是专门寻找纯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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |