Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:06:46+00:00 2026-05-27T08:06:46+00:00

I’d building an app that uses hashtags, like Twitter or Tweetbot. When you’re typing

  • 0

I’d building an app that uses hashtags, like Twitter or Tweetbot. When you’re typing a message, if you type the hashtag symbol, I’d like to suggest tags that match the current one you’re typing.

I’ve already figured out how to get the UITableView to appear and show a list of hashtags, but what I can’t figure out is how to do the following:

  1. Get the NSRange of the current word being typed,
  2. See if that range is formatted like a hashtag (NSRegularExpression @"#\\w\\w*")
  3. (From here on out, I’ve got the code figured out to search for matching hashtags and show them in the UITableView)

Can anyone help me with steps 1 and 2? I’ve been thinking about using textViewDidChange:, but I’m concerned that the app’s performance might suffer if I’m constantly running methods every time the characters change.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:06:47+00:00Added an answer on May 27, 2026 at 8:06 am

    I figured it out! I wound up using the textViewDidChange: and textViewDidChangeSelection: methods.

    To get the NSRange of the current hashtag being typed, I ran a for loop over the NSRegularExpression matches in the text string. From there, I used NSLocationInRange to find out if the current cursor position intersected any of the hashtags.

    Here’s the code:

    //Get the ranges of current hashtags
    NSArray *hashtagRanges = [StringChecker rangesOfHashtagsInString:textView.text];
    NSTextCheckingResult *currentHashtag;
    
    if ([hashtagRanges count] >0)
    {
        //List the ranges of all the hashtags
        for (int i = 0; i<[hashtagRanges count]; i++) 
        {
            NSTextCheckingResult *hashtag = [hashtagRanges objectAtIndex:i];
            //Check if the currentRange intersects the hashtag
            //Have to add an extra space to the range for if you're at the end of a hashtag. (since NSLocationInRange uses a < instead of <=)
            NSRange currentlyTypingHashtagRange = NSMakeRange(hashtag.range.location, hashtag.range.length + 1);
            if (NSLocationInRange(currentRange.location, currentlyTypingHashtagRange))
            {
                //If the cursor is over the hashtag, then snag that hashtag for matching purposes.
                currentHashtag = hashtag;
            }
        }
    
        if (currentHashtag){
            //If we found one hashtag that we're currently editing
    
            //Display the hashtag suggester, feed it the current hashtag for matching.
            [self showTagTable];
    
            //Get the current list of hashtags into an array
            NSFetchRequest *hashtagRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription *tagEntityDescription = [NSEntityDescription entityForName:@"Tags" 
                                                                    inManagedObjectContext:self.note.managedObjectContext];
            [hashtagRequest setEntity:tagEntityDescription];
            NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateLastUsed" 
                                                                             ascending:YES];
            NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
            [hashtagRequest setSortDescriptors:sortDescriptors];
    
            NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", [noteTextView.text substringWithRange:currentHashtag.range]];
            [hashtagRequest setPredicate:tagPredicate];
    
            tagsToDisplay = (NSMutableArray *)[self.note.managedObjectContext executeFetchRequest:hashtagRequest error:nil];
            [tagListTable reloadData];
    
            //If there are no matching hashtags, then let's hide the tag table.
            if ([tagsToDisplay count] == 0) 
            {
                [self hideTagTable];
                return;
            }
    
        }
    

    The StringChecker class is a custom one that I wrote, it just has class methods that parse the strings. I made StringChecker a class because the methods are used in several places in the app. Here’s the method:

        #pragma mark - Hashtag Methods
    +(NSArray *)rangesOfHashtagsInString:(NSString *)string {
        NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:@"#\\w\\w*" 
                                                                                    options:NSRegularExpressionCaseInsensitive 
                                                                                      error:nil];    
        NSArray *hashtagRanges = [hashtagDetector matchesInString:string
                                                          options:NSMatchingWithoutAnchoringBounds
                                                            range:NSMakeRange(0, string.length)];
        return hashtagRanges;
    }
    
    +(NSUInteger)numberOfHashtagsInString:(NSString *)string {
        NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:@"#\\w\\w*" 
                                                                                    options:NSRegularExpressionCaseInsensitive 
                                                                                      error:nil];
        NSUInteger numberOfHashtags = [hashtagDetector numberOfMatchesInString:string
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         range:NSMakeRange(0, string.length)];
        return numberOfHashtags;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.