5

我想选择然后用特定颜色突出显示标签上的相同文本。这可以在手势的帮助下实现吗?而且我必须存储突出显示部分的位置,即使应用程序终端,所以当用户回来时,他们可以看到突出显示的部分

谢谢

4

2 回答 2

6

的,您可以UILabel通过更改背景颜色或UILabel.

您还可以存储您using的当前状态,并在我们用户启动您的应用程序时将其读回。UILabelNSUserDefaults

将状态声明isLabelHighlightedBOOLUILabel

UITapGestureRecognizer* myLabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelClicked:)];
[myLabelView setUserInteractionEnabled:YES];
[myLabelView addGestureRecognizer:myLabelGesture];


-(void)LabelClicked:(UIGestureRecognizer*) gestureRecognizer
{
    if(isLabelHighlighted)
    { 
         myLabelView.highlightedTextColor = [UIColor greenColor];
    }
    else 
    {
         myLabelView.highlightedTextColor = [UIColor redColor];
    }
}

存储UILabel.

[[NSUserDefaults standardUserDefaults] setBool:isLabelHighlighted forKey:@"yourKey"];

要访问它,您应该在下面使用。

isLabelHighlighted = [[NSUserDefaults standardUserDefaults] boolForKey:@"yourKey"];
于 2011-06-13T15:48:40.737 回答
1

NSUserDefaults不适合,因为应用程序可能会意外终止 UITapGestureRecognizer不支持任何状态,除了UIGestureRecognizerStateEnded

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerAction:)];
    longPressGestureRecognizer.minimumPressDuration = 0.01;
    [label setUserInteractionEnabled:YES];
    [label addGestureRecognizer:longPressGestureRecognizer];
}


- (void)longPressGestureRecognizerAction:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
    {
        label.alpha = 0.3;
    }
    else
    {
        label.alpha = 1.0;

        CGPoint point = [gestureRecognizer locationInView:label];
        BOOL containsPoint = CGRectContainsPoint(label.bounds, point);

        if (containsPoint)
        {
            // Action (Touch Up Inside)
        }
    }
}
于 2016-10-05T12:46:59.857 回答