How to calculate letterSpacing for TextView from sketch values?

☆樱花仙子☆ 提交于 2020-06-11 17:34:12

问题


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?


回答1:


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)




回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/39100969/how-to-calculate-letterspacing-for-textview-from-sketch-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!