Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate letterSpacing for TextView from sketch values?

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?

like image 319
mohax Avatar asked Aug 23 '16 12:08

mohax


2 Answers

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)

like image 119
Eric Avatar answered Nov 07 '22 02:11

Eric


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;
like image 30
Siyamed Avatar answered Nov 07 '22 00:11

Siyamed