[英]how to draw a line on label text in xamarin forms
在這里,您可以自己控制(strikedLabel)並放置此代碼。 為了使它更有趣和靈活,您可以添加可綁定屬性,如(IsStriked,StrikeColor等),請記住標簽和BoxView之間的順序很重要
<Grid>
<Label VerticalOptions="Center" Text="Sample string" />
<BoxView HeightRequest="3"
VerticalOptions="Center"
Opacity="0.5"
Color="Aqua" />
</Grid>
由於Xamarin.Forms不提供開箱即用的此功能,因此您需要通過為每個平台創建自定義渲染器來擴展Label。 通過這樣做,您可以訪問每個平台上的本機控件(Android上的TextView和iOS上的UILabel )並在那里實現刪除線效果。
someTextView.PaintFlags = Paint.StrikeThruText;
如果您不熟悉自定義渲染器,請查看有關如何在此處自定義Entry控件的入門教程。
編輯:實際上,你甚至可以使用效果來解決這個問題。 更多信息在這里 。
使用效果非常簡單執行以下步驟:
XAML
<Label Text="{Binding TotalReatilAmount}" >
<Label.Effects>
<Helpers:LineThroughEffect />
</Label.Effects>
</Label>
不要忘記在XAML標題中添加以下行
xmlns:Helpers="clr-namespace:MyNameSpace.Helpers"
創建一個繼承自RoutingEffect的類
public class LineThroughEffect : RoutingEffect
{
public LineThroughEffect() : base("MyNameSpace.Helpers.PlatformLineThroughEffect") { }
}
為Platform Specific創建渲染
iOS版
[assembly: ResolutionGroupName("MyNameSpace.Helpers")]
[assembly: ExportEffect(typeof(PlatformLineThroughEffect), nameof(PlatformLineThroughEffect))]
namespace MyNameSpace.iOS.Renderers
{
public class PlatformLineThroughEffect : PlatformEffect
{
protected override void OnAttached()
{
SetUnderline(true);
}
protected override void OnDetached()
{
SetUnderline(false);
}
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);
if (args.PropertyName == Label.TextProperty.PropertyName ||
args.PropertyName == Label.FormattedTextProperty.PropertyName)
{
SetUnderline(true);
}
}
private void SetUnderline(bool underlined)
{
try
{
var label = (UILabel)Control;
if (label != null)
{
var text = (NSMutableAttributedString)label.AttributedText;
if (text != null)
{
var range = new NSRange(0, text.Length);
if (underlined)
{
text.AddAttribute(UIStringAttributeKey.StrikethroughStyle,
NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);
}
else
{
text.RemoveAttribute(UIStringAttributeKey.StrikethroughStyle, range);
}
}
}
}
catch (Exception ex)
{
ex.Track();
}
}
}
}
我認為,不是使用不同的布局或框視圖實現它,我們可以簡單地使用Xamarin提供的Label的TextDecorations屬性,如下所示。
<Label
Text="$200"
TextDecorations="Strikethrough" />
我正在將我的Android端實現添加到此答案中。 我從這篇文章改編了它: https : //smstuebe.de/2016/08/29/underlinedlabel.xamarin.forms/
using Android.Graphics;
using Android.Widget;
using MyNamespace.Core.CustomRenderers;
using MyNamespace.Droid.Library.ViewRenderers;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ResolutionGroupName(StrikethroughEffect.EffectNamespace)]
[assembly: ExportEffect(typeof(StrikethroughEffectAndroid), nameof(StrikethroughEffect))]
namespace MyNamespace.Droid.Library.ViewRenderers
{
public class StrikethroughEffectAndroid : PlatformEffect
{
protected override void OnAttached()
{
RenderEffect(true);
}
protected override void OnDetached()
{
RenderEffect(false);
}
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);
if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
{
RenderEffect(true);
}
}
private void RenderEffect(bool shouldApply)
{
try
{
var textView = (TextView)Control;
if (shouldApply)
{
textView.PaintFlags |= PaintFlags.StrikeThruText;
}
else
{
textView.PaintFlags &= ~PaintFlags.StrikeThruText;
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot strike-through Label. Error: ", ex.Message);
}
}
}
}
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.