如何在Xamarin上添加双下划线标签?

时间:2020-04-30 07:05:18

标签: xamarin xamarin.forms xamarin.android xamarin.ios

我要添加一些文字和单词,以增加双下划线的效果。

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

我尝试在带有标签的FlexLayout中使用BoxView,但是由于这个原因,出现了自动换行问题。

<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 个答案:

答案 0 :(得分:1)

您可以使用 Custom Renderer (自定义渲染器)并在特定平台上实施。

此外,默认情况下,iOS中可以使用双下划线。但是在Android中,它不可用。

在iOS中

this.setState({
              data:responseData
            })

在Android中

样式用于单个下划线。

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;
            }

        }
    }
}
相关问题