Last active
March 29, 2016 19:49
-
-
Save thedoapps/a66fb2ec9e8755642985 to your computer and use it in GitHub Desktop.
Custom TextView Android with spacing between letters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pe.taxiapp.client.widget; | |
/** | |
* Created by doapps on 9/9/15. | |
*/ | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Typeface; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.text.style.ScaleXSpan; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
import pe.taxiapp.client.R; | |
public class RegularText extends TextView { | |
public RegularText(Context context) { | |
super(context); | |
init(context, null); | |
} | |
public RegularText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public RegularText(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs); | |
} | |
private void init(Context ctx, AttributeSet attrs) { | |
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/gyreadregular.otf"); | |
setTypeface(typeface); | |
setTextColor(getResources().getColor(R.color.white)); | |
getAttrsText(ctx, attrs); | |
setSpacing(1); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.Spacing.NORMAL | |
} | |
/** | |
* Metodos para generar el espaciado* | |
*/ | |
private float spacing = Spacing.NORMAL; | |
private CharSequence originalText = ""; | |
public class Spacing { | |
public final static float NORMAL = 0; | |
} | |
public float getSpacing() { | |
return this.spacing; | |
} | |
public void setSpacing(float spacing) { | |
this.spacing = spacing; | |
applySpacing(); | |
} | |
@Override | |
public void setText(CharSequence text, BufferType type) { | |
originalText = text; | |
applySpacing(); | |
} | |
@Override | |
public CharSequence getText() { | |
return originalText; | |
} | |
private void applySpacing() { | |
if (this == null || this.originalText == null) return; | |
StringBuilder builder = new StringBuilder(); | |
for (int i = 0; i < originalText.length(); i++) { | |
builder.append(originalText.charAt(i)); | |
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((spacing + 1) / 22), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); | |
} | |
} | |
super.setText(finalText, BufferType.SPANNABLE); | |
} | |
/** | |
* Metodo para obtener el atributo* | |
*/ | |
private void getAttrsText(Context ctx, AttributeSet attrs) { | |
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.ButtonGeneralText); | |
String customFont = a.getString(R.styleable.ButtonGeneralText_customTextForm); | |
originalText = customFont; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good!