我想选择然后用特定颜色突出显示标签上的相同文本。这可以在手势的帮助下实现吗?而且我必须存储突出显示部分的位置,即使应用程序终端,所以当用户回来时,他们可以看到突出显示的部分
谢谢
我想选择然后用特定颜色突出显示标签上的相同文本。这可以在手势的帮助下实现吗?而且我必须存储突出显示部分的位置,即使应用程序终端,所以当用户回来时,他们可以看到突出显示的部分
谢谢
是的,您可以UILabel
通过更改背景颜色或UILabel
.
您还可以存储您using的当前状态,并在我们用户启动您的应用程序时将其读回。UILabel
NSUserDefaults
将状态声明isLabelHighlighted
为BOOLUILabel
。
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"];
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)
}
}
}