使用AddAttribute的下划线和删除线无法正确更新

时间:2018-07-20 18:39:14

标签: c# xamarin xamarin.ios custom-renderer

我正在iOS上制作自定义渲染器,以获取UILabel的Underline和StrikeThrough。 使用

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

工作完美。

但使用AddAttribute不会更新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;
}

我在没有运气的情况下试图做不同的事情 每次更改选项时,仅使用新的NSMutableAttributedString对象

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

1 个答案:

答案 0 :(得分:0)

答案实际上是一种解决方法

Xamarin.ios NSUnderlineStyle.Single中的

不起作用,我改用NSUnderlineStyle.Think

stringAttributes.AddAttribute()无法正常工作,所以我坚持使用Control.AttributedText = stringAttributes.

我明白了,但是如果我使用多行文本删除线不起作用, 显然,这是iOS中的一个已知问题:https://github.com/lionheart/openradar-mirror/issues/17165 这是xamarin的解决方法 http://www.openradar.me/31174934