Hash tag detection for TextViews in Swift
This is a quick and dirty sample project implementing Hashtag detection
The approach used here is to add an attribute to hashtag words, much like an "href".
Before doing anything, we need to detect URLs.
- In the storyboard, make sure the TextView can detect Links
- For the above to work, de-select Editable
At this point, URLs in the textview will open in the Safari app.
Next, add an "href" attribute to each hashtagged word.
The overall process goes something like this:
- Iterate over each word and look for anything that starts with
#
- Grab the string, and chop off the
#
character. For example,#helloWorld
becomeshelloWorld
- Create a fake URL using a fake URL scheme. For example,
fakeScheme:helloWorld
- Associate this fake URL with the hashtag word.
NSMutableAttributedString
has methods to accomplish this.
Now that hashtags are URLs, they are a different color and can be clicked. Note: there's no need to add a tap gesture to the TextView, because it's already listening for URL clicks.
Intercept the URL click and check for your fake URL scheme.
- Set the TextView delegate.
- Implement the
UITextFieldDelegate
which has ashouldInteractWithURL
method - Check for your fake
URL.scheme
- Grab the payload in the
URL.resourceSpecifier
- STTweetLabel, an Objective-C CocoaPod for hashtag detection
- A swift implementation of hashtags and mentions. I wish this was available when I first implemented hashtags. I won't be offended if you use it!
- I used this to initially figure out my approach. Just click "translate from japanese" on the top.
- I used this to figure out how to use
NSMutableAttributedString
- Ray Wenderlich Scroll View video series helped me understand keyboard movement. It's tricky! Video subscription is pay per month, but worth it.