0

I'm new in iOS development and here's my problem. In my app, I've some looong texts to show, but in those texts, I want some words to be underlined and "active". What I mean by "active" is that I can click on one of those words to popup a definition or a picture.

I thought using UITextView and UILabel such that I use a Textview until I get an active word (which I show thanks to a label connected to a popup) then I use a Textview again.

If i want this : bla bla bla bla ACTIVE bla bla bla bla ...

I would use : TextView Label TextView ...

Besides that, I've no idea how to do this and I realise this is a pretty bad way of doing it.

My texts and words have to be in an XML file and I'm not sur about the best structure to use...

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • What part are you having troubles with? You seem to allude to you already having the text parsing done, but then at the end it seems like you are asking how to do that part. Have you thought about doing this all in HTML inside a UIWebView? Then you could have hyperlinks instead of UILabels and the parsing and selecting of text would be pretty much built in. – Putz1103 Jan 31 '13 at 15:20
  • I didn't have done the parsing yet. It was just an idea. I'm going to look at UIWebView, that seems good ! As I said, I'm new to iOS dev, I didn't even know about UIWebView ;) – aurélien lebeau Jan 31 '13 at 17:13

1 Answers1

0

If you would like to implement this functionality using UITextView then it is not that much tough. There must be some logic to implement this kind of thing. Here are some ideas for this.

If you want underlined word, draw-line from selected word's rect.

For getting selected word from UITextView,

-(void)textViewDidChangeSelection:(UITextView *)textView
{
    @try
    {
        NSLog(@"%@",[textView.text substringWithRange:textView.selectedRange]);
    }
    @catch (NSException *exception)
    {

    }
}

For showing popup from that position use again UITextView's Delegate method - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text and under that just get cursor position and present popup from that location. Something like this.

AutoCompleteView *autoCompleteView = [[AutoCompleteView alloc] initWithContent:suggestions forText:text];
autoCompleteView.delegate = self;
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
CGRect frame = CGRectMake(cursorPosition.x, cursorPosition.y+30, 150, 100);
popoverController = [[WEPopoverController alloc] initWithContentViewController:autoCompleteView];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:frame inView:commentView  permittedArrowDirections:(UIPopoverArrowDirectionUp) animated:YES];

Hope you get bit idea for your requirement.

Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59