Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Created July 19, 2018 18:48
Show Gist options
  • Save arthtilva/797a31eef6a597a6ac8efb8ce34a4bf4 to your computer and use it in GitHub Desktop.
Save arthtilva/797a31eef6a597a6ac8efb8ce34a4bf4 to your computer and use it in GitHub Desktop.
Vertical TextView
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
public class VerticalTextView extends android.support.v7.widget.AppCompatTextView {
final boolean topDown;
public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
final int gravity = getGravity();
if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
topDown = false;
} else
topDown = true;
applyCustomFont(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
if (topDown) {
canvas.translate(getWidth(), 0);
canvas.rotate(90);
} else {
canvas.translate(0, getHeight());
canvas.rotate(-90);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
private void applyCustomFont(Context context, AttributeSet attrs) {
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
setTypeface(selectTypeface(context, textStyle));
}
private Typeface selectTypeface(Context context, int textStyle) {
switch (textStyle) {
case Typeface.BOLD:
return Typeface.createFromAsset(context.getAssets(), "ProzaLibre-Medium.ttf");
case Typeface.NORMAL:
return Typeface.createFromAsset(context.getAssets(), "ProzaLibre-Regular.ttf");// regular
default:
return Typeface.createFromAsset(context.getAssets(), "ProzaLibre-Regular.ttf");// regular
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment