mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 01:43:14 +08:00
feat(css): text-stroke support (#10399)
closes https://github.com/NativeScript/NativeScript/issues/3597 closes https://github.com/NativeScript/NativeScript/issues/3972
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user