1

The idea is for example there is a UILabel (like in Instagram) "User1 started following User2" I want that when we click either on chunk of text 'User1' or 'User2' it does some action(Not opening like usual link in UIWebView)

Tried TTTAttributedLabel , didn't find anything which will fit me exactly.

3 Answers3

3

in case you haven't found something yet, you could also try ActiveLabel.swift which is an UILabel drop-in replacement supporting Hashtags (#), Mentions (@) and URLs (http://) written in Swift.

enter image description here

Here is a simple example:

import ActiveLabel

let label = ActiveLabel()

label.text = "This is a post with #hashtags and a @userhandle."
label.hashtagColor = .blueColor()
label.handleHashtagTap { print("Success. You just tapped the \($0) hashtag") }

Disclaimer: I'm the author of the library.

schickling
  • 4,000
  • 4
  • 29
  • 33
0

Just make a new button. It can do everything a label can do, and also have actions. You can easily update the text of a button by using:

exampleButton.setTitle("example", forState: .Normal)

And you can add an action like this:

    @IBAction func myExample(sender: UIButton) {

    //do action

    }

Hope this helped.

brimstone
  • 3,370
  • 3
  • 28
  • 49
0

Using a method will force you to make two of them (User 1 and User 2) and align it every time you draw it. I think it's better to make a category ( or Swift extension) of UILabel that gets it's frame and add a gesture recognizer with that frame bounds

Christian
  • 382
  • 2
  • 11
  • I thought of that :) But seems like I finally managed to find a proper solution using TTTAttributedLabel here http://stackoverflow.com/a/8926077/4229173 . The idea is to attach a fake hyperlink to to button ,but when link is clicked it will do some action instead of opening UIWebView. – Grisha Gevorkyan Mar 06 '15 at 12:31