I have this Custom Text Stroke Renderer in my project, but after using it I can no longer manually set or bind my TextColor with Hex Colors or System Color Options in XAML and ViewModel. My text always has default TextColor which is Gray.
#CustomTextStroke.cs
namespace Project11.CustomForms
{
public class CustomTextStroke : Label
{
}
}
#StrokeTextView.cs
namespace Project11.CustomForms
{
public class StrokeTextView : TextView
{
private TextView borderText = null;
public StrokeTextView(Context context) : base(context)
{
borderText = new TextView(context);
init();
}
public StrokeTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
borderText = new TextView(context, attrs);
init();
}
public StrokeTextView(Context context, IAttributeSet attrs, int defStyle) : base(context,
attrs, defStyle)
{
borderText = new TextView(context, attrs, defStyle);
init();
}
public void init()
{
TextPaint tp1 = borderText.Paint;
tp1.StrokeWidth = 5; // sets the stroke width
tp1.SetStyle(Style.Stroke);
borderText.SetTextColor(Color.White); // set the stroke color
borderText.Gravity = Gravity;
}
public override ViewGroup.LayoutParams LayoutParameters { get => base.LayoutParameters;
set => base.LayoutParameters = value; }
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
string tt = borderText.Text;
if (tt == null || !tt.Equals(this.Text))
{
borderText.Text = Text;
this.PostInvalidate();
}
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
borderText.Measure(widthMeasureSpec, heightMeasureSpec);
}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
base.OnLayout(changed, left, top, right, bottom);
borderText.Layout(left, top, right, bottom);
}
protected override void OnDraw(Canvas canvas)
{
borderText.Draw(canvas);
base.OnDraw(canvas);
}
}
}
#CustomTextStrokeRenderer
[assembly: ExportRenderer(typeof(CustomTextStroke), typeof(CustomTextStrokeRenderer))]
namespace Project11.Droid.CustomRenderer
{
public class CustomTextStrokeRenderer : LabelRenderer
{
Context context;
public CustomTextBorderRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null)
{
StrokeTextView strokeTextView = new StrokeTextView(context);
strokeTextView.Text = e.NewElement.Text;
SetNativeControl(strokeTextView);
}
}
}
}
#XAML
<customforms:CustomTextStroke Scale="1.2" Text="Please Enter Here!" FontSize="22"
TextColor="Cyan" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"
/>
<customforms:CustomTextStroke Scale="1.2" Text="Confirm" FontSize="22"
TextColor="{Binding ColorChange}" VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
/>
