Как добавить двойное подчеркивание на ярлык Xamarin? - PullRequest
0 голосов
/ 30 апреля 2020

У меня есть текст, и к некоторым словам я хочу добавить эффект двойного подчеркивания.

<Label Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>

Я пытался использовать BoxView в FlexLayout с меткой, но из-за этого есть слово проблема переноса.

<FlexLayout JustifyContent="Start" AlignContent="Start" AlignItems="Start" FlowDirection="LeftToRight" Wrap="Wrap" >
    <Label Text="Lorem " FontAttributes="Italic"/>
    <StackLayout Spacing="0">
        <Label Text="ipsum" FontAttributes="Italic"/>
        <BoxView WidthRequest="2" BackgroundColor="#747474" Color="#747474" HeightRequest="0.5"/>
        <BoxView WidthRequest="2" BackgroundColor="#747474" Color="#747474" Margin="0,2,0,0" HeightRequest="0.5"/>
    </StackLayout>
    <Label Text=" dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit." FontAttributes="Italic"/>
</FlexLayout>

enter image description here

1 Ответ

1 голос
/ 30 апреля 2020

Вы можете использовать Custom Renderer и реализовать его на определенных c платформах.

Кроме того, двойное подчеркивание доступно в iOS по умолчанию. Но в Android он недоступен.

в iOS

using Foundation;
using UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using App34.iOS;

[assembly:ExportRenderer(typeof(Label),typeof(MyLabelRenderer))]

namespace App34.iOS
{
    public class MyLabelRenderer:LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {
                var content = Control.Text;

                UIStringAttributes attributes = new UIStringAttributes() { UnderlineStyle = NSUnderlineStyle.Double,UnderlineColor=UIColor.Red };

                NSMutableAttributedString str = new NSMutableAttributedString(content, attributes);

                Control.AttributedText = str;
            }

        }
    }
}

в Android

Стиль для одиночного подчеркивания.


using Android.Content;

using Android.Widget;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

using App34.Droid;
using Android.Text;


[assembly: ExportRenderer(typeof(Label), typeof(MyLabelRenderer))]
namespace App34.Droid
{
    public class MyLabelRenderer : LabelRenderer
    {
        public MyLabelRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {

               Control.SetText(Html.FromHtml("<u>" + Control.Text +"</u>",FromHtmlOptions.ModeLegacy),TextView.BufferType.Normal);
            }

        }

    }

}
...