dorianbay
dorianbay

Reputation: 21

Clicking & Editing Words of a Sentence in Label

I have a few sentences that are being shown via a label (NSTextField). These sentences have different words, and I want the user to click on each word an then, accordingly, display different sorts of information depending what the user has clicked on.

Now this seems to be impossible to do with a Label, if I have understood this correctly. What alternatives do I have? I thought about putting every word on a separate NSButton (and change the design of the buttons to not look like Buttons anymore). But there must be an easier solution to this?

Upvotes: 2

Views: 107

Answers (1)

Joe Hallenbeck
Joe Hallenbeck

Reputation: 1450

There is no native decision for this task, but you look in the correct direction:

1) It is necessary to trace the word which the user pressed

2) It is necessary to show view with choice options

I had similar a task, I made so:

I used UITextView to show some text, after i detect the word which the user pressed: I add TapGesture on my UITextView, and disable "editable" in UITextView

- (IBAction)gestureAction:(UITapGestureRecognizer *)sender
{
    _textView =  (UITextView *)sender.view;
    CGPoint location = [sender locationInView:_textView];
    CGPoint position = CGPointMake(location.x, location.y);

    //get location in text
    UITextPosition *tapPosition = [_textView closestPositionToPoint:position];

    //word position 
    UITextRange *textRange = [_textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
    NSString *tappedSentence = [_textView textInRange:textRange];
    NSLog(@"selected :%@ -- %@",tappedSentence,tapPosition);

   if ([tappedSentence isEqualToString:@"Lorem"])
   {
      //show choise view
  }
}

For example, if tapped word is equal to "Lorem", i show pickerView with some choose, and after(i know selected text position) i simply replace this word, if there are questions, i can answer...

P.S sorry I didn't notice that a question about OS X programming, but may be it helpped to you too

Upvotes: 1

Related Questions