Xamarin 如何添加/创建标签笔划?
我正试图在我的Xamarin表单应用程序中添加一个笔划标签,起初我认为这很简单,但在谷歌搜索了几个小时后,我什么也找不到 我所需要的只是具有轮廓/笔划颜色和厚度属性的相同简单标签控件,如下所示:Xamarin 如何添加/创建标签笔划?,xamarin,xamarin.forms,label,stroke,Xamarin,Xamarin.forms,Label,Stroke,我正试图在我的Xamarin表单应用程序中添加一个笔划标签,起初我认为这很简单,但在谷歌搜索了几个小时后,我什么也找不到 我所需要的只是具有轮廓/笔划颜色和厚度属性的相同简单标签控件,如下所示: 提前感谢。您是否希望获得如下屏幕截图所示的结果 如果是这样,您可以创建自定义渲染器来实现它 [assembly: ExportRenderer(typeof(MyCustomLabel), typeof(MyLabelRenderer))] namespace LabelUpdateDemo.i
提前感谢。您是否希望获得如下屏幕截图所示的结果 如果是这样,您可以创建自定义渲染器来实现它
[assembly: ExportRenderer(typeof(MyCustomLabel), typeof(MyLabelRenderer))]
namespace LabelUpdateDemo.iOS
{
class MyLabelRenderer: LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
MyCustomLabel myCustomLabel= Element as MyCustomLabel;
if (Control != null)
{
UIStringAttributes strokeTextAttributes = new UIStringAttributes();
// Here is set the StrokeColor
strokeTextAttributes.StrokeColor = Color.FromHex(myCustomLabel.StrokeColor).ToUIColor(); ;
//Here is set the StrokeThickness, IOS is diferert from the android, it border is set to the inside the font.
strokeTextAttributes.StrokeWidth = -1*myCustomLabel.StrokeThickness;
Control.AttributedText = new NSAttributedString(Control.Text, strokeTextAttributes);
Control.TextColor = UIColor.Black;
}
}
}
}
在Android中,您可以创建两个文本视图
,使较小的文本视图位于较大的文本视图之上。这是代码。
在IOS中,您可以通过相同的方式实现它
[assembly: ExportRenderer(typeof(Label), typeof(MyLabelRenderer))]
namespace LabelUpdateDemo.Droid
{
public class MyLabelRenderer: LabelRenderer
{
Context context;
public MyLabelRenderer(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,Control.TextSize);
strokeTextView.Text = e.NewElement.Text;
strokeTextView.SetTextColor(Android.Graphics.Color.Purple);
// Control.TextSize=50;
SetNativeControl(strokeTextView);
}
}
}
}
下面是xamarin表单中的代码
<Label
x:Name = "BTTextStatus"
FontAttributes="Bold"
Text="Label"
FontSize="Large"/>
然后在xaml
文件夹中使用它
<labelupdatedemo:MyCustomLabel
x:Name = "BTTextStatus"
StrokeColor="#0FBEFE"
StrokeThickness="5"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
TextColor="#0B1751"
FontSize="Medium"/>
在IOS中,您可以使用以下自定义渲染器来实现它
[assembly: ExportRenderer(typeof(MyCustomLabel), typeof(MyLabelRenderer))]
namespace LabelUpdateDemo.iOS
{
class MyLabelRenderer: LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
MyCustomLabel myCustomLabel= Element as MyCustomLabel;
if (Control != null)
{
UIStringAttributes strokeTextAttributes = new UIStringAttributes();
// Here is set the StrokeColor
strokeTextAttributes.StrokeColor = Color.FromHex(myCustomLabel.StrokeColor).ToUIColor(); ;
//Here is set the StrokeThickness, IOS is diferert from the android, it border is set to the inside the font.
strokeTextAttributes.StrokeWidth = -1*myCustomLabel.StrokeThickness;
Control.AttributedText = new NSAttributedString(Control.Text, strokeTextAttributes);
Control.TextColor = UIColor.Black;
}
}
}
}
[程序集:ExportRenderer(typeof(MyCustomLabel)、typeof(MyLabelRenderer))]
名称空间LabelUpdateDemo.iOS
{
类MyLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
MyCustomLabel MyCustomLabel=作为MyCustomLabel的元素;
if(控件!=null)
{
UIStringAttributes strokeTextAttributes=新的UIStringAttributes();
//这里设置了StrokeColor
strokeTextAttributes.StrokeColor=Color.FromHex(myCustomLabel.StrokeColor).ToUIColor();
//这里是设置StrokeThickness,IOS不同于android,它的边框设置为字体内部。
strokeTextAttributes.StrokeWidth=-1*myCustomLabel.StrokeHickness;
Control.AttributedText=新的NSAttributedString(Control.Text,strokeTextAttributes);
Control.TextColor=UIColor.Black;
}
}
}
}
是否希望获得如下屏幕截图所示的结果
如果是这样,您可以创建自定义渲染器来实现它
[assembly: ExportRenderer(typeof(MyCustomLabel), typeof(MyLabelRenderer))]
namespace LabelUpdateDemo.iOS
{
class MyLabelRenderer: LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
MyCustomLabel myCustomLabel= Element as MyCustomLabel;
if (Control != null)
{
UIStringAttributes strokeTextAttributes = new UIStringAttributes();
// Here is set the StrokeColor
strokeTextAttributes.StrokeColor = Color.FromHex(myCustomLabel.StrokeColor).ToUIColor(); ;
//Here is set the StrokeThickness, IOS is diferert from the android, it border is set to the inside the font.
strokeTextAttributes.StrokeWidth = -1*myCustomLabel.StrokeThickness;
Control.AttributedText = new NSAttributedString(Control.Text, strokeTextAttributes);
Control.TextColor = UIColor.Black;
}
}
}
}
在Android中,您可以创建两个文本视图
,使较小的文本视图位于较大的文本视图之上。这是代码。
在IOS中,您可以通过相同的方式实现它
[assembly: ExportRenderer(typeof(Label), typeof(MyLabelRenderer))]
namespace LabelUpdateDemo.Droid
{
public class MyLabelRenderer: LabelRenderer
{
Context context;
public MyLabelRenderer(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,Control.TextSize);
strokeTextView.Text = e.NewElement.Text;
strokeTextView.SetTextColor(Android.Graphics.Color.Purple);
// Control.TextSize=50;
SetNativeControl(strokeTextView);
}
}
}
}
下面是xamarin表单中的代码
<Label
x:Name = "BTTextStatus"
FontAttributes="Bold"
Text="Label"
FontSize="Large"/>
然后在xaml
文件夹中使用它
<labelupdatedemo:MyCustomLabel
x:Name = "BTTextStatus"
StrokeColor="#0FBEFE"
StrokeThickness="5"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
TextColor="#0B1751"
FontSize="Medium"/>
在IOS中,您可以使用以下自定义渲染器来实现它
[assembly: ExportRenderer(typeof(MyCustomLabel), typeof(MyLabelRenderer))]
namespace LabelUpdateDemo.iOS
{
class MyLabelRenderer: LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
MyCustomLabel myCustomLabel= Element as MyCustomLabel;
if (Control != null)
{
UIStringAttributes strokeTextAttributes = new UIStringAttributes();
// Here is set the StrokeColor
strokeTextAttributes.StrokeColor = Color.FromHex(myCustomLabel.StrokeColor).ToUIColor(); ;
//Here is set the StrokeThickness, IOS is diferert from the android, it border is set to the inside the font.
strokeTextAttributes.StrokeWidth = -1*myCustomLabel.StrokeThickness;
Control.AttributedText = new NSAttributedString(Control.Text, strokeTextAttributes);
Control.TextColor = UIColor.Black;
}
}
}
}
[程序集:ExportRenderer(typeof(MyCustomLabel)、typeof(MyLabelRenderer))]
名称空间LabelUpdateDemo.iOS
{
类MyLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
MyCustomLabel MyCustomLabel=作为MyCustomLabel的元素;
if(控件!=null)
{
UIStringAttributes strokeTextAttributes=新的UIStringAttributes();
//这里设置了StrokeColor
strokeTextAttributes.StrokeColor=Color.FromHex(myCustomLabel.StrokeColor).ToUIColor();
//这里是设置StrokeThickness,IOS不同于android,它的边框设置为字体内部。
strokeTextAttributes.StrokeWidth=-1*myCustomLabel.StrokeHickness;
Control.AttributedText=新的NSAttributedString(Control.Text,strokeTextAttributes);
Control.TextColor=UIColor.Black;
}
}
}
}
使用SkiaSharp或自定义渲染器最后一个例子是文本:好吧,我已经检查过了,但是SkiaSharp对于这样一个任务来说是非常复杂和过分的。自定义渲染器确实是首选,但我希望有人能为我制作,因为我对Xamarin还不太熟练。使用SkiaSharp或自定义渲染器最后一个例子是文本:好吧,我已经检查过了,但是SkiaSharp对于这样的任务来说是非常复杂和过分的。一个自定义的渲染器确实是首选,但我想如果有人可以为我,因为我还没有那么熟练的Xamarin。你能帮我更多吗?比如给出Android、IOS和PCL的完整代码。另外,最后的代码,其中颜色不是硬编码的,而是从xaml提供给控件的。提前谢谢。我已经给出了你的解决方案,你自己试试,你会学到很多东西。@MohamedAshraf这是我的演示。尽管我找到了解决我的问题的办法,但我也很尊重你的努力,你能告诉我如何在IOS上做同样的事情吗?我会非常高兴的,因为我使用的是Xamarin表单,你可以看到^^你能帮我更多吗?比如给出Android、IOS和PCL的完整代码。另外,最后的代码,其中颜色不是硬编码的,而是从xaml提供给控件的。提前谢谢。我已经给出了你的解决方案,你自己试试,你会学到很多东西。@MohamedAshraf这是我的演示。尽管我找到了解决我的问题的办法,但我也很尊重你的努力,你能告诉我如何在IOS上做同样的事情吗?我会非常高兴的,因为我正在使用Xamarin表单,正如你们所看到的^^