¿Cómo hacer el espaciado entre letras en Android TextView?
Necesito hacer el diseño de la disposición del photoshop. Hay algunas fuentes en los diseños. El diseñador me dio estas fuentes. Pero en la disposición en photoshop, él utiliza el espaciamiento entre letras. ¿Cómo puedo realizar esto en android textView? He encontrado una solución:
Respuesta en stackoverflow
- Autosizing de TextView no funciona (Android O)
- ¿Puedo limitar el número de caracteres de TextView?
- Cómo encontrar el área de texto (altura / ancho) de TextView de forma programática en android
- ¿Por qué TextView.setTypeface () aumenta exageradamente mi altura TextView cuando uso la fuente Roboto-Italic.ttf?
- Agregar Layout de forma programática dentro de otro
Responde dos
Pero si hago myTextView extends TextView
que trabaja mal. Si me adapto a un ingenio, en el dispositivo con mayor visibilidad, el espaciado entre letras aumenta no es proporcional.
EDITAR
public class MyTextView extends TextView { private float letterSpacing = 0.0f; private CharSequence originalText = ""; private Typeface typeface; public MyTextView(Context context) { this(context, null); isInEditMode(); } public MyTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); isInEditMode(); } public MyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray attributesArray = getResources().obtainAttributes(attrs, R.styleable.MyTextView); letterSpacing = attributesArray.getDimension(R.styleable.MyTextView_letterSpacing, 0.0f); String fontName = attributesArray.getString(R.styleable.MyTextView_fontName); if(!this.isInEditMode()) { if (null == fontName) { typeface = Fonts.getBlockBertholdRegular(context); } else { typeface = Fonts.get(context, fontName); } super.setTypeface(typeface); } originalText = super.getText(); applyLetterSpacing(); this.invalidate(); } public float getLetterSpacing() { return letterSpacing; } public void setLetterSpacing(float letterSpacing) { this.letterSpacing = letterSpacing; applyLetterSpacing(); } @Override public void setText(CharSequence text, BufferType type) { originalText = text; applyLetterSpacing(); } @Override public CharSequence getText() { return originalText; } private void applyLetterSpacing() { if (this == null || this.originalText == null) return; StringBuilder builder = new StringBuilder(); for (int i = 0; i < originalText.length(); i++) { String c = "" + originalText.charAt(i); builder.append(c.toUpperCase()); 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((letterSpacing + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } super.setText(finalText, BufferType.SPANNABLE); if(!this.isInEditMode()) { super.setTypeface(typeface); } } }
- ¿Revestimientos Android para Spannable?
- ¿Cómo mostrar HTML en TextView?
- Limitar el número de líneas en TextView
- Android: cómo dar bulletpoints, salto de línea al texto en textview
- Más espacio entre líneas en TextView
- Romper el texto grande en las páginas en el selector de texto androide o ver flipper
- El interfuncionamiento de TextView
- ¿Cómo obtengo espacios pequeños como separador de números en Android?
Intente usar el nuevo método API de setLetterSpacing
. Mira aquí
EDITAR
También puede crear su propia fuente con espacios dentro de la fuente en sí y aplicarla a su TextView .
Desde API 21 puede utilizar
setLetterSpacing
La documentación se puede encontrar aquí
- ViewGroup {TextView, …} .getMeasuredHeight da valor incorrecto es menor que la altura real
- ¿Hay alguna alternativa para ActionBar en 2.2?