How to create UILabel with clickable keyword

I want to create a shortcut in iOS, can someone help me make the first word of the shortcut text bold and interactive. The username and comment are displayed on the label, and the first word is always the username. Thanks in advance!

+7
ios objective-c iphone uilabel ipad
Mar 08 '13 at 11:55 on
source share
5 answers

I assume a more elegant solution would use TTTAttributedString or the like.

Example:

simple demo

Output:

2013-03-10 07:16:54.429 ClickableUILabel-SO[4770:c07] UserName clicked Address: { comment = "Your comment."; userName = user2126537; } 2013-03-10 07:16:55.460 ClickableUILabel-SO[4770:c07] UserName clicked Address: { comment = "Another comment."; userName = nsgulliver; } 

Key points:

 ... NSRange userNameRange = [text rangeOfString: userName]; ... label.delegate = self; [label addLinkToAddress: @{ @"userName" : userName, @"comment" : comment } withRange: userNameRange]; ... - (void) attributedLabel: (TTTAttributedLabel *)label didSelectLinkWithAddress: (NSDictionary *)addressComponents { NSLog(@"UserName clicked\nAddress:\t%@", addressComponents); } 

Full source code

Note that you must open xcworkspace in Xcode / AppCode, because here I use CocoaPods .

Hope this helps.

BR.
Evgeniy

+10
Mar 10 '13 at 3:27
source share

You need to use UITapGestureRecognizer to create a clickable UILabel . Use UIView and add UILabel as subtitles for this

 UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourMethod:)]; [yourLabelView setUserInteractionEnabled:YES]; [yourLabelView addGestureRecognizer:gesture]; 

One way to make the first word clickable is to output the first word from the label using the string method and save it on another label and use the code above to make it interactive

 NSArray* wordArray = [yourLabel.text componentsSeparatedByString: @" "]; NSString* firstWord = [wordArray objectAtIndex: 0]; 
+4
Mar 08 '13 at 11:59
source share
  • Create a custom button that will contain the first word of your username, make the text bold.
  • Take the label next to the user button and write the rest of your username except the first word.
  • In the custom button click event, do whatever you want to do.

I hope this will become clear to you.

Enjoy the programming!

+2
Mar 08 '13 at 12:03
source share

shortcuts seem difficult. Ypu can use a view. add a button and a labyrinth on this side and add the symbol of the 1st character to the button and the others to the label.

0
Mar 08 '13 at 11:59
source share

You need to add UIGestureRecognizer to your UILabel

 [myLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabelActionPerformed)]]; 

// then you can have your own method

 -(void)myLabelActionPerformed { NSLog(@"I tapped a UILabel"); } 

Remember to make your UILabel UI enabled

-one
Sep 24 '14 at 11:25
source share



All Articles