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