How to Make UITextView Detect and Suggest Hashtags in Your iPhone App?

Hashtags are used by all major social media platforms today. It’s a convenient tool for filtering relevant content.

Today, with hashtags so common, not only people already use them on each social media platform, but they’re also starting to expect these feature in other apps.

In this iPhone app tutorial, we’ll see the step-by-step process of how to add UITextview and then how to make UITextview detect and suggest hashtags in an iPhone app.

Let’s Get Started

Open Xcode and create a new project.


In the next tab, select Single View Application as project type and click on next.


Write your project name and details in the given tab.


Once you successfully create a new project in your XCode, create a Nib file.

To create a Nib file:

  • Right click on project’s root folder & select New File…
  • Select “User Interface” from left side.
  • Select next & give appropriate name and click on “Create” to add .Nib file.

Now, add a UITableView into it.


Create a UIView class & connect a TableView outlet from .Nib into this file.

Add a UITextView into a ViewController in Main.Storyboard


Now, set delegate & datasource of UITextView.


in ViewDidLoad() we have used some properties of UITextView.

override func viewDidLoad() {

textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor.lightGrayColor().CGColor

viewOuter = HashTagTableViewCell.fromNib()
viewOuter.hidden = true
viewOuter.tableView.delegate = self
viewOuter.tableView.dataSource = self

viewOuter.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

arrSearched = arrValues

Populate UITextView with selected value from table.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

viewOuter.hidden = true
let textViewTmp = textView.text

var strNewText = textViewTmp.componentsSeparatedByString("#")
if strNewText.count > 1 {
strNewText[strNewText.count - 1] = arrSearched[indexPath.row]
let strFinalText = strNewText.joinWithSeparator("#")
textView.text = strFinalText.stringByAppendingString(" ")

Now, to search the character & show table if character is found.

func textViewDidChangeSelection(textView: UITextView) {
let str = textView.text
if str != "" {
let lastChar = str[str.endIndex.predecessor()]
if lastChar == "#" {
viewOuter.hidden = false
else {
viewOuter.hidden = true
let textviewtext = textView.text
let new = textviewtext.componentsSeparatedByString("#")
arrSearched = arrValues.filter({$0.lowercaseString.containsString(new.last!.stringByReplacingOccurrencesOfString("#", withString: "").lowercaseString)})

And done!

Once you successfully implement this code, you can easily make any UITextView detect hashtags in your iPhone app. If you’d like to get to more advanced level, it is also possible to detect mentions as well as links in the UI Textview. Though, if you’re planning for your business mobile app, you may hire iPhone app developers to apply this functionality in your app.

Get a free copy of Hashtag UITextView Demo From Github.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.