public class

LetterSpacingSpan

extends MetricAffectingSpan

 java.lang.Object

↳MetricAffectingSpan

↳androidx.text.style.LetterSpacingSpan

Overview

Span used to adjust the letter spacing.

Summary

Constructors
publicLetterSpacingSpan(float letterSpacing)

Constructor of LetterSpacingSpan.

Methods
public voidupdateDrawState(TextPaint textPaint)

public voidupdateMeasureState(TextPaint textPaint)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public LetterSpacingSpan(float letterSpacing)

Constructor of LetterSpacingSpan.

Parameters:

letterSpacing: the extra letter spacing in the unit of EM

Methods

public void updateDrawState(TextPaint textPaint)

public void updateMeasureState(TextPaint textPaint)

Source

package androidx.text.style

import android.text.TextPaint
import android.text.style.MetricAffectingSpan
import androidx.annotation.Px
import androidx.annotation.RestrictTo

/**
 * Span that sets the letter spacing as [letterSpacing], in the unit of pixel.
 *
 * @suppress
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class LetterSpacingSpanPx(@Px val letterSpacing: Float) : MetricAffectingSpan() {
    private fun TextPaint.updatePaint() {
        // In framework, 1em letterSpacing equals to textSize * textScaleX pixels.
        val emWidth = textSize * textScaleX
        // Do nothing if emWidth is 0.0f.
        if (emWidth != 0.0f) {
            letterSpacing = this@LetterSpacingSpanPx.letterSpacing / emWidth
        }
    }

    override fun updateDrawState(textPaint: TextPaint) {
        textPaint.updatePaint()
    }

    override fun updateMeasureState(textPaint: TextPaint) {
        textPaint.updatePaint()
    }
}