如何在Android TextView中调整文本字距?

30

在Android的TextView中,是否有一种方法可以调整字符之间的间距?我相信这通常被称为“字距调整(kerning)”。

我知道android:textScaleX属性,但它会压缩字符和间距。

12个回答

71

我建立了一个自定义类,继承了TextView并添加了一个方法"setSpacing"。解决方法类似于@Noah所说的。该方法在字符串每个字母之间添加空格,并使用SpannedString更改空格的TextScaleX,从而允许正负间距。

/**
 * Text view that allows changing the letter spacing of the text.
 * 
 * @author Pedro Barros (pedrobarros.dev at gmail.com)
 * @since May 7, 2013
 */

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ScaleXSpan;
import android.util.AttributeSet;
import android.widget.TextView;

public class LetterSpacingTextView extends TextView {

    private float spacing = Spacing.NORMAL;
    private CharSequence originalText = "";


    public LetterSpacingTextView(Context context) {
        super(context);
    }

    public LetterSpacingTextView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }

    public float getSpacing() {
        return this.spacing;
    }

    public void setSpacing(float spacing) {
        this.spacing = spacing;
        applySpacing();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        applySpacing();
    }

    @Override
    public CharSequence getText() {
        return originalText;
    }

    private void applySpacing() {
        if (this == null || this.originalText == null) return;
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < originalText.length(); i++) {
            builder.append(originalText.charAt(i));
            if(i+1 < originalText.length()) {
                builder.append("\u00A0");
            }
        }
        SpannableString finalText = new SpannableString(builder.toString());
        if(builder.toString().length() > 1) {
            for(int i = 1; i < builder.toString().length(); i+=2) {
                finalText.setSpan(new ScaleXSpan((spacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        super.setText(finalText, BufferType.SPANNABLE);
    }

    public class Spacing {
        public final static float NORMAL = 0;
    }
}

使用它:

LetterSpacingTextView textView = new LetterSpacingTextView(context);
textView.setSpacing(10); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.Spacing.NORMAL
textView.setText("My text");
//Add the textView in a layout, for instance:
((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);

5
将 builder.append(" ") 替换为不间断空格 builder.append("\u00A0"),问题得到解决。感谢提供代码! - Dasha Salo
谢谢。我尝试过,但只有通过setText()才能使其工作。我已经设置了文本属性并尝试在parseAttrs()上应用applyLetterSpacing(),但得到的是空白视图。我该怎么办? - apito
1
顺便提一下,你可以使用 SpannedStringBuilder 在同一个循环中插入空格并设置跨度! - Takhion
1
谢谢@Sam!终于Google实现了这个功能!已编辑 ;) - Pedro Barros
7
提醒大家,如果有人想使用这个功能,请注意一下。如果你将android:textAllCaps设置为true,它会删除跨度(spans)的宽度,并导致无法正确渲染。 - TrevorSStone
显示剩余5条评论

14

如果有人想要以简单的方式对任何字符串(在技术上是CharSequence)应用调整字距(kerning),而不使用TextView

public static Spannable applyKerning(CharSequence src, float kerning)
{
    if (src == null) return null;
    final int srcLength = src.length();
    if (srcLength < 2) return src instanceof Spannable
                              ? (Spannable)src
                              : new SpannableString(src);

    final String nonBreakingSpace = "\u00A0";
    final SpannableStringBuilder builder = src instanceof SpannableStringBuilder
                                           ? (SpannableStringBuilder)src
                                           : new SpannableStringBuilder(src);
    for (int i = src.length() - 1; i >= 1; i--)
    {
        builder.insert(i, nonBreakingSpace);
        builder.setSpan(new ScaleXSpan(kerning), i, i + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return builder;
}

12

据我所知,在 TextView 中无法调整字距。但如果你使用 2D 图形 API 自己在 Canvas 上绘制文本,则可能可以调整字距。


好的,那就这样吧。干杯,迈克。 - emmby
1
嗨,@CommonsWare,如果我使用2D图形API在画布上绘制文本,如何调整字距?你能给我一些提示吗? - neevek
您可以通过提供自己修改过的字体版本来实现字距调整。 - mvds
2
@CommonsWare 自API21以来,“letterSpacing”执行相同的操作。但是,如何实现向后兼容性?我正在研究库“v7”的样式,但似乎不支持它。您能否请告诉我们您的发现? - Pankaj Kumar
2
不再正确:请查看 android:letterSpacing https://developer.android.com/reference/android/widget/TextView#attr_android:letterSpacing - joerick

7
自Android 21版本以来,您可以使用letterSpacing属性来设置字母间距。
<TextView
    android:width="..."
    android:height="..."
    android:letterSpacing="1.3"/>

4
这是我的解决方案,它在每个字符之间添加了统一的间距(以像素为单位)。这个span假设所有的文本都在同一行。这基本上实现了@commonsWare建议的内容。
SpannableStringBuilder builder = new SpannableStringBuilder("WIDE normal");
builder.setSpan(new TrackingSpan(20), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
...

private static class TrackingSpan extends ReplacementSpan {
    private float mTrackingPx;

    public TrackingSpan(float tracking) {
        mTrackingPx = tracking;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, 
        int start, int end, Paint.FontMetricsInt fm) {
        return (int) (paint.measureText(text, start, end) 
            + mTrackingPx * (end - start - 1));
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, 
        int start, int end, float x, int top, int y, 
        int bottom, Paint paint) {
        float dx = x;
        for (int i = start; i < end; i++) {
            canvas.drawText(text, i, i + 1, dx, y, paint);
            dx += paint.measureText(text, i, i + 1) + mTrackingPx;
        }
    }
}

3
我发现调整字距的唯一方法是创建自定义字体,其中字形前进被改变。

2

当你使用TextView时,调整字符间距是很困难的。但是如果你能够自己处理绘图,应该有一些方法可以实现。

对于这个问题,我的答案是:使用自定义Span

我的代码:

public class LetterSpacingSpan extends ReplacementSpan {
    private int letterSpacing = 0;

    public LetterSpacingSpan spacing(int space) {
        letterSpacing = space;

        return this;
    }


    @Override
    public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
        return (int) paint.measureText(text, start, end) + (text.length() - 1) * letterSpacing;
    }


    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
        int length = text.length();
        float currentX = x;

        for (int i = 1; i < length; i++) {          
            canvas.drawText(text, i, i + 1, currentX, y, paint);
            currentX += paint.measureText(text, i, i + 1) + letterSpacing;
         }
    }
}

解释:

构建自己的Span可以帮助您实现许多惊人的效果,比如使模糊的TextView、更改TextView的背景或前景,甚至制作一些动画。我从这篇文章Span a powerful concept中学到了很多。

因为您正在为每个字符添加间距,所以我们应该使用基于字符级别的Span,在这种情况下,ReplacementSpan是最好的选择。我添加了一个spacing方法,因此在使用它时,您可以简单地将每个字符所需的空格作为参数传递。

构建自定义span时,您需要覆盖至少两个方法:getSizedrawgetSize方法应返回添加整个charsequence的间距后的最终宽度,而在draw方法块内,您可以控制Canvas进行所需的绘制。

那么我们如何使用这个LetterSpacingSpan呢?很简单:

用法:

TextView textView;
Spannable content = new SpannableString("This is the content");
textView.setSpan(new LetterSpacingSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(content);

就是这样。


1

您也可以尝试使用SpannedString,但需要解析它并更改每个单词的字符间距。


1
这个答案可能对想在Canvas上使用drawText绘制字距的人有所帮助(不是关于TextView中的文本)。自Lollipop以来,Paint上可用setLetterSpacing方法。如果SDK为LOLLIPOP及以上,则使用setLetterSpacing。否则,将调用一种类似于@dgmltn上面建议的方法:
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        paint.setLetterSpacing(-0.04f);  // setLetterSpacing is only available from LOLLIPOP and on
        canvas.drawText(text, xOffset, yOffset, paint);
    } else {
        float spacePercentage = 0.05f;
        drawKernedText(canvas, text, xOffset, yOffset, paint, spacePercentage);
    }


/**
 * Programatically drawn kerned text by drawing the text string character by character with a space in between.
 * Return the width of the text.
 * If canvas is null, the text won't be drawn, but the width will still be returned
 * kernPercentage determines the space between each letter. If it's 0, there will be no space between letters.
 * Otherwise, there will be space between each letter. The  value is a fraction of the width of a blank space.
 */
private int drawKernedText(Canvas canvas, String text, float xOffset, float yOffset, Paint paint, float kernPercentage) {
    Rect textRect = new Rect();
    int width = 0;
    int space = Math.round(paint.measureText(" ") * kernPercentage);
    for (int i = 0; i < text.length(); i++) {
        if (canvas != null) {
            canvas.drawText(String.valueOf(text.charAt(i)), xOffset, yOffset, paint);
        }
        int charWidth;
        if (text.charAt(i) == ' ') {
            charWidth = Math.round(paint.measureText(String.valueOf(text.charAt(i)))) + space;
        } else {
            paint.getTextBounds(text, i, i + 1, textRect);
            charWidth = textRect.width() + space;
        }
        xOffset += charWidth;
        width += charWidth;
    }
    return width;
}

0
我想使用@PedroBarros的答案,但要定义间距应该是多少像素。
以下是我对applySpacing方法的编辑:
private void applySpacing() {
    if (this == null || this.originalText == null) return;

    Paint testPaint = new Paint();
    testPaint.set(this.getPaint());
    float spaceOriginalSize = testPaint.measureText("\u00A0");
    float spaceScaleXFactor = ( spaceOriginalSize > 0 ? spacing/spaceOriginalSize : 1);

    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < originalText.length(); i++) {
        builder.append(originalText.charAt(i));
        if(i+1 < originalText.length()) {
            builder.append("\u00A0");
        }
    }
    SpannableString finalText = new SpannableString(builder.toString());
    if(builder.toString().length() > 1) {
        for(int i = 1; i < builder.toString().length(); i+=2) {
            finalText.setSpan(new ScaleXSpan(spaceScaleXFactor), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    super.setText(finalText, BufferType.SPANNABLE);
}

我是一名Android开发的初学者,请随时告诉我如果有不好的地方!


这个 == null,它可能吗? - Luigi Blu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接