My solution requires using a UITextView (which is much simpler, and I highly recommend you use it).
Swift
class ViewController: UIViewController { @IBOutlet weak var textView:UITextView!; override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let gestureRecognizer = UITapGestureRecognizer(target: self, action: "textViewTapped:"); gestureRecognizer.numberOfTapsRequired = 1; gestureRecognizer.numberOfTouchesRequired = 1; self.textView.addGestureRecognizer(gestureRecognizer); } func textViewTapped(sender: UITapGestureRecognizer) { let wordTarget = "here"; let word = UITextView.getWordAtPosition(sender.locationInView(self.textView), textView: self.textView); if word == wordTarget { let plainString = self.textView.attributedText.string; let substrings = NSMutableArray(); let scanner = NSScanner(string: plainString); scanner.scanUpToString("#", intoString: nil); while !scanner.atEnd { var substring:NSString? = nil; scanner.scanString("#", intoString: nil); let space = " "; if scanner.scanUpToString(space, intoString: &substring) { // If the space immediately followed the
Objective-c
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewTapped:)]; gestureRecognizer.numberOfTouchesRequired = 1; gestureRecognizer.numberOfTapsRequired = 1; [self.textView addGestureRecognizer:gestureRecognizer]; } - (void)textViewTapped:(UITapGestureRecognizer *)sender { NSString *wordTarget = @"here"; NSString* word = [self getWordAtPosition:[sender locationInView:self.textView] textView:self.textView]; if ([word isEqualToString:wordTarget]) { NSString *plainString = self.textView.attributedText.string; NSMutableArray* substrings = [[NSMutableArray alloc]init]; NSScanner *scanner = [[NSScanner alloc]initWithString:plainString]; [scanner scanUpToString:@"#" intoString:nil]; while (![scanner isAtEnd]) { NSString* substring = nil; [scanner scanString:@"#" intoString:nil]; NSString* space = @" "; if ([scanner scanUpToString:space intoString:&substring]) { [substrings addObject:substring]; } [scanner scanUpToString:@"#" intoString:nil]; } //Now you got your substrings in an array, so use those for your data passing (in a segue maybe?) ... } } - (NSString*)getWordAtPosition:(CGPoint)position textView:(UITextView *)textView { //remove scrollOffset CGPoint correctedPoint = CGPointMake(position.x, textView.contentOffset.y + position.y); UITextPosition *tapPosition = [textView closestPositionToPoint:correctedPoint]; UITextRange *wordRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight]; return [textView textInRange:wordRange]; }
Basically, you need to add a gesture recognizer to get the transition point in your text view. Then you get the word using the category method specified in the extension area. After that, you check what the word is (where we need the word "here"). Then we collect the hashtags that you provided.
All you have to do is add the performSegueWithIdentifier method and pass it accordingly.
source share