我希望得到一个特定的行为TextBlock
,使其高度只包括大写字母的高度(从基线到顶部减去"上升高度").请参阅图像Sphinx
从维基百科到明白我的意思.此外,下面的图片可能表明我所追求的更好.
我并不是专门寻找纯XAML解决方案(可能是不可能的),所以C#代码(转换器)也很好.
这是XamlPad中用于在上图中生成左A的XAML.
<TextBlock Text="A" Background="Aquamarine" FontSize="120" HorizontalAlignment="Center" VerticalAlignment="Center" />
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用属性 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();
}
}
Run Code Online (Sandbox Code Playgroud)
转换器上使用的系数取决于使用的系列字体(基线和行间距):
<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}}"/>
Run Code Online (Sandbox Code Playgroud)
最好的解决方案是找到如何根据 FontFamily 的参数 Baseline 和 LineSpacing 来计算 Coeff。在此示例 (Segeo UI) 中,Height = 0.715 且 LineHeight = 0,875 * FontSize 的系数。
更新:
如果我理解正确的话,我知道一些技巧,
您可以Scale
使用 RenderTransform 来实现,这通常是最有效的方法;
<TextBlock Text="Blah">
<TextBlock.RenderTransform>
<CompositeTransform ScaleY="3"/>
</TextBlock.RenderTransform>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
或者,您可以嵌入TextBlock
aViewbox
来“缩放”文本以适合其容器的边界,例如您在网格行上设置硬高度值,例如;
<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>
Run Code Online (Sandbox Code Playgroud)
或者您可以将 绑定FontSize
到 Container 元素,例如;
<Grid x:Name="MyText" Height="120">
<TextBlock FontSize="{Binding ElementName=MyText, Path=Height}" Text="Sphinx" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
他们可能会呈现出您想要的效果?
归档时间: |
|
查看次数: |
1077 次 |
最近记录: |