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:
- Get the
NSRangeof the current word being typed, - See if that range is formatted like a hashtag (
NSRegularExpression @"#\\w\\w*") - (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!
I figured it out! I wound up using the
textViewDidChange:andtextViewDidChangeSelection:methods.To get the
NSRangeof the current hashtag being typed, I ran aforloop over theNSRegularExpressionmatches in the text string. From there, I usedNSLocationInRangeto find out if the current cursor position intersected any of the hashtags.Here’s the code:
The
StringCheckerclass is a custom one that I wrote, it just has class methods that parse the strings. I madeStringCheckera class because the methods are used in several places in the app. Here’s the method: