Nathan Walker
2023-10-09 12:00:21 -07:00
committed by GitHub
parent 93e24783a1
commit d6478237ec
18 changed files with 230 additions and 76 deletions

View File

@ -0,0 +1,50 @@
package org.nativescript.widgets;
import android.content.Context;
import android.graphics.Canvas;
import android.text.TextPaint;
/**
* @author NathanWalker
*/
public class StyleableTextView extends androidx.appcompat.widget.AppCompatTextView {
int mTextStrokeWidth = 0;
int mTextStrokeColor = 0;
int mTextColor = 0;
public StyleableTextView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
if (mTextStrokeWidth > 0) {
_applyStroke(canvas);
} else {
super.onDraw(canvas);
}
}
public void setTextStroke(int width, int color, int textColor) {
mTextStrokeWidth = width;
mTextStrokeColor = color;
mTextColor = textColor;
}
private void _applyStroke(Canvas canvas) {
// set paint to fill mode
TextPaint p = this.getPaint();
p.setStyle(TextPaint.Style.FILL);
// draw the fill part of text
super.onDraw(canvas);
// stroke color and width
p.setStyle(TextPaint.Style.STROKE);
p.setStrokeWidth(mTextStrokeWidth);
this.setTextColor(mTextStrokeColor);
// draw stroke
super.onDraw(canvas);
// draw original text color fill back (fallback to white)
p.setStyle(TextPaint.Style.FILL);
this.setTextColor(mTextColor != 0 ? mTextColor : 0xffffffff);
}
}