轻扫手势划过文本

swipe gesture strike through text

我一直在想出一个解决方案,使用向右滑动手势,使 label.text 上的文本具有删除线效果,再次滑动删除删除线,并保持原始文本完好无损.有关如何执行此操作的任何代码示例?这是一个 XCode 问题。

if ([***this is the part i need help with***])
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[self.EditItem valueForKey:@"eventName"]];
    [attributeString addAttribute:NSStrikethroughStyleAttributeName
                            value:@1
                            range:NSMakeRange(0, [attributeString length])];
    self.nameTextField.attributedText = attributeString;
    [self.EditItem setValue:[NSString stringWithFormat:@"Completed"] forKey:@"eventName"];
    NSLog(@"Swiped to the right");
}
else
{
    [NSString initWithString:[self.EditItem valueForKey:@"eventName"]];
    NSLog(@"normal text no strike through");
}

您可以尝试像这样的简单手势识别器..

在您的 viewDidLoad 或类似内容中添加以下内容

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(strikeThrough)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;

[self.nameTextField addGestureRecognizer:swipe];

然后设置 strikeThrough 方法将文本更改为删除线 - 如果您只有一个文本字段,只需添加一个切换按钮,以便您可以打开和关闭删除线。

bool stricken;

- (void) strikeThrough {

    if (stricken) {

        self.nameTextField.text = self.nameTextField.text;

        stricken = false;

    } else {

        NSDictionary* attributes = @{ NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
        NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:self.nameTextField.text attributes:attributes];
        self.nameTextField.attributedText = attrText;

        stricken = true;

  }

}