【问题标题】:Is there any way that I can add an underscore to text in a Xamarin Label?有什么办法可以在 Xamarin 标签中为文本添加下划线吗?
【发布时间】:2023-01-08 12:48:31
【问题描述】:

对于这个例子:

var vm.MyText = "ABC";

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

有没有办法可以在文本中添加下划线?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    使用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://learn.microsoft.com/en-us/xamarin/xamarin-forms/"/>
                         </Span.GestureRecognizers>
                    </Span>
                    <Span Text="." />
                </FormattedString.Spans>
            </FormattedString>
        </Label.FormattedText>
    </Label>
    

    【讨论】:

      【解决方案2】:

      您可以使用 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;
      

      回复:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text

      【讨论】:

        【解决方案3】:

        编辑:如果您运行的是 3.3.0 之前的 XF 版本,请检查此答案,否则请遵循已接受的答案。

        如果你需要的是下划线,你必须创建一个效果

        使用 Xamarin.Forms;

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

        安卓实现

        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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-24
          • 2021-12-21
          • 1970-01-01
          • 2014-08-19
          • 2014-02-01
          • 1970-01-01
          • 2012-09-07
          • 2012-01-23
          相关资源
          最近更新 更多