首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大写字母大小的TextBlock (忽略字体升降)

大写字母大小的TextBlock (忽略字体升降)
EN

Stack Overflow用户
提问于 2012-03-20 18:50:47
回答 2查看 1.2K关注 0票数 8

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

我并不是专门寻找一个纯粹的XAML解决方案(可能是不可能的),所以一个C#代码(一个转换器)也是可以的。

这是在XamlPad中用来生成上图中左A的XAML。

代码语言:javascript
复制
<TextBlock Text="A" Background="Aquamarine" FontSize="120" HorizontalAlignment="Center" VerticalAlignment="Center" />
EN

回答 2

Stack Overflow用户

发布于 2013-04-08 18:09:34

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

代码语言:javascript
复制
// 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):

代码语言:javascript
复制
<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。

票数 4
EN

Stack Overflow用户

发布于 2012-03-21 00:50:01

更新:

如果我没理解错的话,我知道一些诀窍,

你可以用RenderTransform来Scale它,这通常是最有效的方法;

代码语言:javascript
复制
<TextBlock Text="Blah">
  <TextBlock.RenderTransform>
   <CompositeTransform ScaleY="3"/>
  </TextBlock.RenderTransform>
</TextBlock>

或者,您可以将TextBlock嵌入到Viewbox中,以“缩放”文本以适应其容器的边界,例如,如果您在网格行上设置了硬高度值,如;

代码语言:javascript
复制
<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绑定到容器元素,如;

代码语言:javascript
复制
<Grid x:Name="MyText" Height="120">
<TextBlock FontSize="{Binding ElementName=MyText, Path=Height}" Text="Sphinx" />
</Grid>

它们可能会呈现出你想要的效果?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9785322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档