Selection and highlighting text on a label
Asked Answered
B

2

5

I want to select and then highlight the same text on the label with a particular color.Is this be achievable with the help of gestures. And i have to store the position of the highlighted part,even if the application terminas,so when the user comes back ,they can see that part highlighted

Thanks

Busterbustle answered 13/6, 2011 at 15:45 Comment(0)
Y
6

Yes, you could use the gesture with your UILabel for highlighting text by either changing the background color or text color of your UILabel.

You could also store the current state of your UILabel using NSUserDefaults , and read it back we user launch your application.

Declare an isLabelHighlighted as BOOL for UILabel state.

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];
    }
}

To store state of your UILabel.

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

To access it, you should use below.

isLabelHighlighted = [[NSUserDefaults standardUserDefaults] boolForKey:@"yourKey"];
Yawning answered 13/6, 2011 at 15:48 Comment(1)
did you get answere for this? please sharePivoting
R
1

NSUserDefaults is not suitable, because the application can be terminated unexpectedly UITapGestureRecognizer not supported any states, except 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)
        }
    }
}
Revisionist answered 5/10, 2016 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.