1

I have an attributed string set up like this:

var range = (showStr as NSString).rangeOfString(spellingStr)
var attributedString = NSMutableAttributedString(string:showStr)
attributedString.addAttribute(NSForegroundColorAttributeName, value: ezGreen, range: range)

and i would like to add a tap gesture just to the range that i set to green.

Is there an attribute for touches? How would i set the tap gesture just for the spellingStr part?

EDIT

all my code for that label and string is below:

var showStr:NSString = "Showing results for \(searchT)."
println("SearchTerm:\(searchT)")
showingLabel.textColor = .blackColor()

var range = (showStr as NSString).rangeOfString(searchT)
var attributedString = NSMutableAttributedString(string:showStr)
attributedString.addAttribute(NSForegroundColorAttributeName, value: ezGreen , range: range)

showingLabel.attributedText = attributedString
showingLabel.font = UIFont.systemFontOfSize(17)
showingLabel.numberOfLines = 0
showingLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
var showHeight:CGFloat = heightForView(showingLabel.text!, UIFont.systemFontOfSize(17), maxLabelWidth)
showingLabel.frame = CGRectMake(20, heightOfCor, maxLabelWidth, showHeight)
heightOfCor += showHeight
bgBlack.addSubview(showingLabel)
inVINCEable
  • 2,167
  • 3
  • 25
  • 49
  • This might get you started: http://stackoverflow.com/questions/18680724/how-can-i-detect-taps-on-a-particular-part-substring-of-a-uilabel – Glynbeard Nov 20 '14 at 14:49
  • This may be helpful to you http://stackoverflow.com/questions/1256887/clickable-links-in-uilabel – Alex Andrews Nov 20 '14 at 15:49
  • @Glynbeard the problem i have with that solution is i don't want to recreate a label to click, because the string can be very different each time. – inVINCEable Nov 20 '14 at 16:26
  • So, let me clarify sth. You want to be able to tap on some parts of the string in `UITextView` and be able to handle the event? – yusuke024 Nov 20 '14 at 16:31
  • @SikhapolSaijit Exactly, except it's a `UILabel` – inVINCEable Nov 20 '14 at 16:37
  • All of the code you've provided relates to the model (the string), and not the view (the UILabel). Could you add the related UILabel as well? – A. R. Younce Nov 20 '14 at 17:29
  • @A.R.Younce added, sorry i missed that – inVINCEable Nov 20 '14 at 17:32
  • 1
    This seems to provide a good answer: http://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview-on-ios-7 – edwardmp Nov 20 '14 at 17:34
  • 1
    @inVINCEable If it's possible for you to use `UITextView` instead (you can disable editing which will make it undistinguishable to the user), you can follow the answers provided by @edwardmp to achieve what you want easily. – yusuke024 Nov 20 '14 at 17:44

1 Answers1

4

I've done it by just creating a UIButton and setting it to the exact frame of that line of text.

Here's an example of how to do it in objective-c:

NSRange range = [self.textView.text rangeOfString:@"The string of text you want to tap"];
self.textView.selectedRange = range;
UITextRange *textRange = [self.textView selectedTextRange];
CGRect textRect = [self.textView firstRectForRange:textRange];
CGRect convertedRect = [self.view convertRect:textRect fromView:self.textView];

UIButton *button = [[UIButton alloc]initWithFrame:convertedRect];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:@selector(textTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view bringSubviewToFront:button];

NOTE: prior to calling this, you should have already setup your attributed string and added it to your UITextView. Also, ensure you allow selection inside your textView (but disable userInteraction if you dont want highlights etc)

enter image description here

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75