ラベル上の同じテキストを特定の色で選択して強調表示したいのですが、これはジェスチャーを使用して実現できますか?そして、アプリケーションが終了した場合でも、強調表示された部分の位置を保存する必要があるため、ユーザーが戻ってきたときに、強調表示された部分を見ることができます
ありがとう
ラベル上の同じテキストを特定の色で選択して強調表示したいのですが、これはジェスチャーを使用して実現できますか?そして、アプリケーションが終了した場合でも、強調表示された部分の位置を保存する必要があるため、ユーザーが戻ってきたときに、強調表示された部分を見ることができます
ありがとう
はい、UILabel
の背景色またはテキストの色を変更することで、テキストを強調表示するためにジェスチャーを使用できますUILabel
。
また、使用中の現在の状態を保存し、それを読み返して、ユーザーがアプリケーションを起動することもできます。UILabel
NSUserDefaults
状態のBOOLisLabelHighlighted
として宣言します。UILabel
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)
}
}
}