Search code examples
c#xamarinxamarin.ioscustom-renderer

Underline and Strikethrough using AddAttribute not updating properly


I am making a custom renderer on iOS to get Underline and StrikeThrough for UILabel. Using

var stringAttributes = new NSMutableAttributedString(Control.Text,new UIStringAttributes{ UnderlineStyle = NSUnderlineStyle.Thick, UnderlineColor = UIColor.Black, BackgroundColor = UIColor.Brown});

Worked perfectly .

but using AddAttribute not updating the UiLabel

{
    var stringAttributes = new NSMutableAttributedString(Control.AttributedText);
    if (OutField.FontAttr.CrossedOut)
    {
        stringAttributes.AddAttribute(new NSString("UnderlineStyle"),
                                      NSNumber.FromInt32((int)NSUnderlineStyle.Thick),
            new NSRange(0, Element.Text.Length));
        stringAttributes.AddAttribute(new NSString("UnderlineColor"),
                                      Control.TextColor,
                                      new NSRange(0, Element.Text.Length));
    }
    if (OutField.FontAttr.Underlined)
    {
        stringAttributes.AddAttribute(new NSString("UnderlineStyle"),
            NSNumber.FromInt32((int)NSUnderlineStyle.Single),
                                      new NSRange(0, Element.Text.Length));
        stringAttributes.AddAttribute(new NSString("UnderlineColor"),
                                      Control.TextColor,
    new NSRange(0, Element.Text.Length));
    }
    Control.AttributedText = stringAttributes;
}

I tried to do same thing differently without any luck Using just new NSMutableAttributedString object every time i change the options

NSMutableAttributedString stringAttributes = null;
if (OutField.FontAttr.CrossedOut && OutField.FontAttr.Underlined)
{
    stringAttributes = new NSMutableAttributedString(Control.Text, new UIStringAttributes
    {
        UnderlineStyle = NSUnderlineStyle.Single,
        UnderlineColor = Control.TextColor,
        StrikethroughStyle = NSUnderlineStyle.Single,
        StrikethroughColor = Control.TextColor
    });
}
else if (OutField.FontAttr.Underlined)
{
    stringAttributes = new NSMutableAttributedString(Control.Text, new UIStringAttributes
    {
        UnderlineStyle = NSUnderlineStyle.Single,
        UnderlineColor = Control.TextColor,
        StrikethroughStyle = NSUnderlineStyle.Single,
        StrikethroughColor = Control.TextColor
    });
}
else if (OutField.FontAttr.CrossedOut)
{
    stringAttributes = new NSMutableAttributedString(Control.Text, new UIStringAttributes
    {
        UnderlineStyle = NSUnderlineStyle.Single,
        UnderlineColor = Control.TextColor,
        StrikethroughStyle = NSUnderlineStyle.Single,
        StrikethroughColor = Control.TextColor
    });
}
if (stringAttributes != null)
{
    Control.AttributedText = stringAttributes;
}

Solution

  • The answer is actually a workaround

    in Xamarin.ios NSUnderlineStyle.Single doesn't work i used NSUnderlineStyle.Think instead.

    stringAttributes.AddAttribute() couldnt get it to work as well so i stick with Control.AttributedText = stringAttributes.

    I got it figured but if i use multiline text strikethrough doesn't work and apparently its a known issue in iOS : https://github.com/lionheart/openradar-mirror/issues/17165 And this is the workaround for xamarin http://www.openradar.me/31174934