'Bug - TextDecoration Strikethrough not working on iOS

I have a label where I want strikethrough and a lineheight of 1.5 This works fine with Android, but strikethrough does not work on iOS when LineHeight is also set.

Steps to Reproduce

 - Create new Xamarin.Forms project.
 - Add label with textdecoration=strikethrough 
 - Test strikethrough
 - Run emulator

Expected Behavior

  • Display of label with Strikethrough text

Actual Behavior

  • Text displayed normally, not with Strikethrough on iOS (Android works fine)


Solution 1:[1]

I resolved this issue with a label custom renderer...

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace YourApp.iOS.Renderers
{
    public class CustomLabelRenderer : LabelRenderer
    {
        private readonly IDictionary<TextDecorations, NSString> attributeNames =
            new Dictionary<TextDecorations, NSString> {
                {
                    TextDecorations.Underline,
                    UIStringAttributeKey.UnderlineStyle },
                {
                    TextDecorations.Strikethrough,
                    UIStringAttributeKey.StrikethroughStyle
                }
            };

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (sender is Label label &&
                label.FormattedText?.Spans.ToList() is List<Span> spans)
            {
                var aux = new NSMutableAttributedString(Control.AttributedText);
                spans.ForEach(span => {
                    if (attributeNames.TryGetValue(span.TextDecorations,
                        out var attributeName))
                    {
                        var startIndex = Control.Text.IndexOf(span.Text);
                        aux.AddAttribute(
                            attributeName,
                            new NSNumber(1),
                            new NSRange(startIndex, span.Text.Length));
                    }
                });
                Control.AttributedText = aux;
            }
        }
    }
}

This works for Underline and Strikethrough.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriel Ocampo Lastra