5

ラベル上の同じテキストを特定の色で選択して強調表示したいのですが、これはジェスチャーを使用して実現できますか?そして、アプリケーションが終了した場合でも、強調表示された部分の位置を保存する必要があるため、ユーザーが戻ってきたときに、強調表示された部分を見ることができます

ありがとう

4

2 に答える 2

6

はい、UILabelの背景色またはテキストの色を変更することで、テキストを強調表示するためにジェスチャーを使用できますUILabel

また、使用中の現在の状態を保存し、それを読み返して、ユーザーがアプリケーションを起動することもできます。UILabelNSUserDefaults

状態の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"];
于 2011-06-13T15:48:40.737 に答える
1

NSUserDefaultsUITapGestureRecognizerアプリケーションが予期せず終了する可能性があるため、適切ではありません 。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 に答える