有什么方法可以在Xamarin标签中的文本上添加下划线?

时间:2018-11-06 14:58:50

标签: xamarin xamarin.forms

对于此示例:

var vm.MyText = "ABC";

<Label Grid.Row="1" Text="{Binding MyText}" />

是否可以在文本中添加下划线?

3 个答案:

答案 0 :(得分:2)

使用TextDecorations="Underline"(需要3.3.0)

<Label>
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="This app is written in C#, XAML, and native APIs using the" />
                <Span Text=" " />
                <Span Text="Xamarin Platform" FontAttributes="Bold" TextColor="Blue" TextDecorations="Underline">
                    <Span.GestureRecognizers>
                       <TapGestureRecognizer 
                            Command="{Binding TapCommand, Mode=OneWay}"
                            CommandParameter="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/"/>
                     </Span.GestureRecognizers>
                </Span>
                <Span Text="." />
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
</Label>

答案 1 :(得分:0)

您可以使用FormattedString将不同的属性,颜色等应用于标签:

var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "Stack, ", FontAttributes = FontAttributes.None });
formattedString.Spans.Add(new Span { Text = "Overflow, ", FontAttributes = FontAttributes.Italic });
label.FormattedText = formattedString;

re:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text

答案 2 :(得分:-2)

如果需要下划线,则必须创建效果

使用Xamarin.Forms;

namespace YourProjectNamespace.Effects
{
    public class UnderlineTextEffect : RoutingEffect
    {
        public UnderlineTextEffect()
            : base("YourProjectNamespace.UnderlineTextEffect")
        {
        }
    }
}

Android实现

using System;
using System.ComponentModel;
using Android.Graphics;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ResolutionGroupName("YourProjectNamespace")]
[assembly: ExportEffect(typeof(AndroidUnderlineTextEffect), "UnderlineTextEffect")]
namespace YourProjectNamespace.Android.Effects
{
    public class AndroidUnderlineTextEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            ((TextView)Control).PaintFlags |= PaintFlags.UnderlineText;
        }

        protected override void OnDetached()
        {
        }

        protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged(args);

            if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
                ((TextView)Control).PaintFlags |= PaintFlags.UnderlineText;
        }
    }
}

iOS实施:

using System;
using System.ComponentModel;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ResolutionGroupName("YourProjectNamespace")]
[assembly: ExportEffect(typeof(AppleUnderlineTextEffect), "UnderlineTextEffect")]
namespace YourProjectNamespace.iOS.Effects
{
    public class AppleUnderlineTextEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            SetTextUnderline();
        }

        protected override void OnDetached()
        {
        }

        protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged(args);

            if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
                SetTextUnderline();
        }

        private void SetTextUnderline()
        {
            var text = ((UILabel)Control).AttributedText as NSMutableAttributedString;
            var range = new NSRange(0, text.Length);

            text.AddAttribute(UIStringAttributeKey.UnderlineStyle,
                                  NSNumber.FromInt32((int)NSUnderlineStyle.Single),
                                  range);
        }
    }
}

然后在您的XAML上添加效果:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:YourProjectNamespace"
    x:Class="YourProjectNamespace.UnderlineEffectPage">
    <StackLayout>
        <Label
            HorizontalOptions="FillAndExpand"
            VerticalOptions="CenterAndExpand"
            Text="Underlined Text">
            <Label.Effects>
                <local:UnderlineTextEffect />
            </Label.Effects>
        </Label>
    </StackLayout>
</ContentPage>