如何在 WPF 文本框中指定字母间距或字距? [英] How can I specify letter spacing or kerning, in a WPF TextBox?

查看:197
本文介绍了如何在 WPF 文本框中指定字母间距或字距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改 WPF 文本框中字符之间的间距.
类似于 CSS 中可用的 letter-spacing: 5px 的东西.
我认为在 XAML 中是可能的;最简单的方法是什么?

I'd like to modify the spacing between characters in a WPF TextBox.
Something like the letter-spacing: 5px thing that is available in CSS.
I think it is possible in XAML; what's the simplest way?

我找到了GlyphRun 对象和 Glyphs 元素简介"文件,并发现它非常无用.

I found the "Introduction to the GlyphRun Object and Glyphs Element" document, and found it to be exceedingly unhelpful.

这是该页面的代码示例:

This is a code example from that page:

<!-- "Hello World!" with explicit character widths for proportional font -->
<Glyphs 
   FontUri             = "C:WINDOWSFontsARIAL.TTF"
   FontRenderingEmSize = "36"
   UnicodeString       = "Hello World!"
   Indices             = ",80;,80;,80;,80;,80;,80;,80;,80;,80;,80;,80"
   Fill                = "Maroon"
   OriginX             = "50"
   OriginY             = "225"
/>

同一文档页面对 Indices 属性的作用给出了以下解释":

The same documentation page gives this "explanation" for what the Indices property does:

我不知道这意味着什么.我也不确定索引是正确的——代码中的注释谈到了我不关心的字符宽度".我想调整 个字符之间的宽度.

I have no idea what any of that means. I'm also not sure that Indices is the right thing - the comment in the code speaks of "character widths" which I don't care about. I want to adjust the width between characters.

此外,没有关于如何将 Glyphs 元素应用于 TextBox 的示例.当我尝试它时,我的 WPF 测试应用程序崩溃了.

Also, there is no example for how to apply a Glyphs element to a TextBox. When I tried it, my WPF test app just crashed.

我想要做的是稍微增加 WPF 文本框中绘制字符之间出现的空白空间.文本的长度和内容会有所不同.每次有新字符时我都必须修改 Indicies 属性吗?有没有办法说每个角色都比平时多 20% 的空间".

What I want to do is slightly increase the empty space that appears between drawn characters within a WPF TextBox. The text will vary in length and content. Do I have to modify the Indicies property every time there is a new character? Is there a way to say "make it 20% more space than usual, for every character".

有人可以帮我吗?

推荐答案

我尝试了 Glyphs 和 FontStretch,但无法轻松获得我想要的结果.我能够想出一种适合我的目的的方法.也许它也适用于其他人.

I tried Glyphs and FontStretch and couldn't easily get the result I was looking for. I was able to come up with an approach that works for my purposes. Maybe it will work for others, as well.

<ItemsControl ItemsSource="{Binding SomeString}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" 
                       Margin="0,0,5,0"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我可以绑定到任何字符串,并且不需要进行任何字符宽度检测来正确设置间距.右边距是字母之间的空格.

I can bind to any string and don't need to do any character width detection to set the spacing properly. The right margin is the space between the letters.

例子:

这篇关于如何在 WPF 文本框中指定字母间距或字距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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