16

In design I have a text field with 16 as text size and 0.6 as character spacing

But, if I set this value for android:letterSpacing attribute of TextView spacing will be much more larger than in design.

So, what is the way to convert sketch value to android value?

mohax
  • 4,435
  • 2
  • 38
  • 85

4 Answers4

30

According to Romain Guy, Android uses the "1 em = font size" definition, so you'd just do

setLetterSpacing(characterSpacing / textSize)

...just make sure those values are in the same unit (dp vs px)

Eric
  • 5,323
  • 6
  • 30
  • 35
3

If Sketch provides the letter spacing value in Points, then the following is the conversion:

float textSizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,  textSizeInSp, displayMetrics);
float letterSpacingPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT,  sketchLetterSpacing, displayMetrics); 
float androidLetterSpacing = letterSpacingPx / textSizePx
return androidLetterSpacing;
Siyamed
  • 495
  • 2
  • 11
2

Material design 2 uses em as the unit for letter spacing on Android, and the following conversion ratio for Sketch values need to be applied:

(Tracking from Sketch / font size in sp) = letter spacing

To clarify, here is an example for Jetpack Compose:

fontSize = 14.sp,
letterSpacing = (1.25 / 14).em

1.25 is the Sketch value and 14 is the font size in sp, but note the unit is converted to em.

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
-2

Check out android:textScaleX

Depending on how much spacing you need, this might help. That's the only thing remotely related to letter-spacing in the TextView.

  • Irrelevant. For instance, in Sketch I have 0.58. It means that I have 0 + .58. So it spacing should be bigger than original ( 0 ) spacing. If I use textScaleX, so I change scale. In this case 0.58 < 1, so scaling will be less, than original. – Slava May 10 '17 at 07:32