如何调整 Android TextView 中的文本字距?
有没有办法调整 Android TextView
中字符之间的间距?我相信这通常称为“字距调整”。
我知道 android:textScaleX
属性,但是它会压缩字符以及间距。
Is there a way to adjust the spacing between characters in an Android TextView
? I believe this is typically called "kerning".
I'm aware of the android:textScaleX
attribute, but that compresses the characters along with the spacing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我构建了一个扩展 TextView 并添加方法“setSpacing”的自定义类。解决方法与@Noah 所说的类似。该方法在字符串的每个字母之间添加一个空格,并使用 SpannedString更改空格的 TextScaleX,允许正空格和负空格。
使用它:
I built a custom class that extends TextView and adds a method "setSpacing". The workaround is similar to what @Noah said. The method adds a space between each letter of the String and with SpannedString changes the TextScaleX of the spaces, allowing positive and negative spacing.
Using it:
如果有人正在寻找一种简单的方法来将字距调整应用于任何字符串(技术上,
CharSequence
)而不使用TextView
:If anyone is looking for a simple way to apply the kerning to any string (technically,
CharSequence
) without using aTextView
:AFAIK,您无法在
TextView
中调整字偶距。如果您自己使用 2D 图形 API 在Canvas
上绘制文本,则可以调整字距调整。AFAIK, you cannot adjust kerning in
TextView
. You may be able to adjust kerning if you draw the text on theCanvas
yourself using the 2D graphics APIs.从 Android 21 开始,您可以使用设置
letterSpacing
属性。Since Android 21, you can use set the
letterSpacing
attribute.这是我的解决方案,它在每个字符之间添加统一的间距(以像素为单位)。此范围假设所有文本都在一行中。这基本上实现了@commonsWare 的建议。
Here's my solution, which adds uniform spacing (in pixels) between each character. This span assumes all text is in a single line. This basically implements what @commonsWare suggests.
我发现调整字偶间距的唯一方法是创建一种自定义字体,其中的字形提前会发生变化。
The only way I found to adjust the kerning, is to create a custom font in which the glyph advance is altered.
当您使用 TextView 时,很难调整字符之间的间距。但如果你可以自己处理绘图,应该有一些方法可以做到这一点。
我对这个问题的回答是:使用您的自定义 Span 。
我的代码:
说明:
构建自己的
Span
可以帮助您实现许多令人惊叹的效果,例如模糊 TextView、更改背景或前景你的TextView,甚至制作一些动画。我从这篇文章中学到了很多东西Span a强大的概念。因为要为每个字符添加间距,所以我们应该使用字符级基本跨度,在这种情况下,ReplacementSpan 是最佳选择。我添加了一个
spacing
方法,因此在使用它时,您只需将每个字符所需的空格作为参数传递即可。构建自定义跨度时,您需要重写至少两个方法:
getSize
和draw
。在我们为整个字符序列添加间距后,getSize
方法应该返回最终宽度,并且在draw
方法块内,您可以控制Canvas
画出你想要的图画。那么我们如何使用这个LetterSpacingSpan呢?这很简单:
用法:
就是这样。
It's difficult to adjust spacing between characters, when you are using TextView. But if you can handle the drawing yourself, there should be some way to do that.
My answer to this question is: use your custom Span .
My code:
Explain:
Building your own
Span
can help you achieve many amazing effect, like make a blur TextView, change the background or foreground for your TextView, even make some animation. I learn a lot from this post Span a powerful concept .Because you are adding spacing to each character, so we should use a character level base span, in this case, ReplacementSpan is the best choice. I add a
spacing
method, so when using it, you can simply pass the space you want for each character as parameter.When building your custom span, you need to override at least two method,
getSize
anddraw
. ThegetSize
method should return the final width after we add the spacing for the whole charsequence, and inside thedraw
method block, you can control theCanvas
to do the drawing you want.So how we use this LetterSpacingSpan? It's easy:
Usage:
And that's it.
您还可以尝试使用 SpannedString 但您需要解析它并更改每个单词的字符间距
You can also try using a SpannedString but you would need to parse it and change the character spacing for each of the words
这个答案可能对那些想要使用drawText在Canvas上绘制带有字距调整的文本的人有帮助(这与TextView中的文本无关)。
从 Lollipop 开始,Paint 上提供了 setLetterSpacing 方法。如果 SDK 为 LOLLIPOP 且已打开,则使用 setLetterSpacing。否则,将调用一个方法来执行与上面@dgmltn的建议类似的操作:
This answer may be helpful for someone who wants to draw text with kerning on a Canvas, using drawText (this is not about text in a TextView).
Since Lollipop, the method setLetterSpacing is available on Paint. If the SDK is LOLLIPOP and on, setLetterSpacing is used. Otherwise, a method is invoked that does something similar to @dgmltn's suggestion above:
对 @Pedro Barros 的答案进行了小幅编辑。如果您使用 SpannableString 来设置它,例如,如果您想使某些字符具有不同的颜色,那么它会很有用:
There's a small edit of @Pedro Barros answer. It is useful if you use SpannableString to set it, e.g. if you want to make different colors of some characters:
我想使用@PedroBarros 答案,但通过定义像素间距。
这是我对 applySpacing 方法的编辑:
我是 Android 开发人员的初学者,如果这不好,请随时告诉我!
I wanted to use @PedroBarros answer, but by defining what the spacing should be in pixel.
Here's my edit to the applySpacing method :
I'm a beginner as an Android developer, please feel free to let me know if this is not good !
还有一种解决方案。
其中pixel_1dp是XML:
要设置间距,请使用如下代码:
One more solution.
Where pixel_1dp is XML:
To set spacing use code like this: