Reputation: 2169
Is there a way to have UILabels
or UITextViews
with UIButtons
?
Example1: "This Car is yellow and has 4 wheels."
Where Car is the UIButton
and can be anywhere in the sentence.
Upvotes: 0
Views: 421
Reputation: 9387
Use an NSAttributedString
with links at the ranges you want, and display them in TTTAttributedLabel
. You can then get a callback whenever a link is tapped.
Upvotes: 1
Reputation: 8014
I have done this with an app using UITextView.firstRectForRange method. This allows you to obtain the coordinates of text within the UITextView.
I used buttons and magic markers in the text. When rendering I located the magic markers and placed UIButtons over the top. e.g. @"This is the age of the $Train$" would place a button with title @"Train" over the marker.
You could also replace each marker with coloured text recording the locations using RectForRange. Then use touch gestures to see if any of the rectangles were selected.
Upvotes: 0
Reputation: 3146
You could assign a UITapGestureRecognizer to the label to detect the tap:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
Then in your 'labelTapped:' selector method, you calculate the location of the tap within the frame of the label with the gesture object. You would then have to determine the location of the word in the label and see if the touch was in that CGRect.
Upvotes: 0