Select specific text in UITextView to trigger segue

I am trying to add a function to my application where the user can select a PRESPECIFIED text for me to run a segue and get an explanation / information about what the user has selected.

The following text is an example:

Hawaii is the most recent of the 50 US states and joined the Union on August 21, 1959.

The user can select "US states" and get information about the US states.

So can I do this with Xcode or do I need to work with some kind of html editing application? and if so, where would be a good place (online or book) to start learning how to do this?

Thank.

+3


source to share


2 answers


There may be better ways, but I know one way to do what you ask.

  • Create NSMutableAttributedString with display text.
  • Define the range of characters you want to click and using setAttributes: rangeSet add an NSLinkAttributeName attribute with a string value that you can use to identify the clicked area.
  • Create a UITextView and set the attribitedString property to the string above
  • Set some object as a delegate of your UITextView
  • Override textView: shouldInteractWithURL: inRange: method. Return NO here, but do what you want with the url generated from the string you used before.


Here's a piece of code. I'll leave the creation of the UITextView for you.

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableAttributedString *attString = self.textView.attributedText.mutableCopy;
    [attString setAttributes:@{NSLinkAttributeName:@"123"} range:NSMakeRange(0, 10)];
    self.textView.attributedText = attString;
    self.textView.delegate = self;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    NSLog(@"%@", URL);
    return NO;
}

      

+2


source


You can do this in Xcode. You will want to create a segue in your storyboard and add an id to the segue. Then, in one of the methods, UITextViewDelegate

you can check the text in the text view. If it's a match, you can program the segue programmatically using -performSegueWithIdentifier:sender:

. Alternatively, you can create the entire segue programmatically using -segueWithIdentifier:source:destination:performHandler:

.



0


source







All Articles