如何删除标签删除效果

时间:2017-02-06 09:19:17

标签: c# xamarin xamarin.ios xamarin.forms label

我会写一个待办事项列表,项目标签应该在完成后删除,并在撤消时删除。点击罢工有效,但是当它已经击出时我无法删除它。我的方法是在NSMutableAttributedString上删除RemoveAttribute,或者重绘整个标签。

[assembly:ResolutionGroupName(OvanTasks.LabelStrikeOutEffect.EffectNamespace)]
[assembly: ExportEffect(typeof(LabelStrikeOutEffect), nameof(LabelStrikeOutEffect))]
namespace OvanTasks.iOS
{
   public class LabelStrikeOutEffect : PlatformEffect
   {
       public LabelStrikeOutEffect()
       {
       }

       protected override void OnAttached()
       {
          SetStrikeOut(true);
       }

       protected override void OnDetached()
       {
          SetStrikeOut(false);
       }

       protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
       {
           base.OnElementPropertyChanged(args);

           if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
           {
               SetStrikeOut(true);
           }
       }

    private void SetStrikeOut(bool strikeout)
    {
        try
        {
            var label = (UILabel)Control;
            var text = (NSMutableAttributedString)label.AttributedText;
            var range = new NSRange(0, text.Length);


            if (strikeout)
            {
                text.AddAttribute(UIStringAttributeKey.StrikethroughStyle, NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);
            }
            else
            {
                Device.BeginInvokeOnMainThread(() => {
                    text.RemoveAttribute(UIStringAttributeKey.StrikethroughStyle, range);   
                });
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine("Cannot strike out Label. Error: " + ex.Message.ToString());
        }
        finally {
            Control.SetNeedsDisplay();
        }
    }

  }
}

1 个答案:

答案 0 :(得分:0)

好的,我有一个解决方案。我只是用新属性覆盖标签的AttributedText。

public class CustomerTest {
    public static void main(String[] args){
        Customer main; // = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
            }
        main = new Customer(name, itemCost);
        main.printDetails();
        }
    }