In my app I've created a small RSS feed reader that shows the titles of the news. I've simply put the titles in a single string and I'm showing them in a scrolling MarqueeLabel. I'd like to make the titles clickable to show the linked news and I've tried to do so following this answer, but the string is not interactable at all, even if it is highlighted as a hyperlink.
Is there an easy way to make it work? I've tried to look if there are libraries that combine both the scrolling and the linking functionalities, but I couldn't find any.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
[marqueeLabel addGestureRecognizer:tapGesture];
-(void)handleTap:(UITapGestureRecognizer *)gesture{
MarqueeLabel *marqueeLabel = (MarqueeLabel*) [gesture view];
NSLog(#"Clicked Link would be %#",marqueeLabel.text);
}
NB:Just add UITapGestureRecognizer to your desired label and handle it
Related
Right now, I have two different UILabels each with their own long press and pan UIGestureRecognizers (setup through the storyboard). My final goal is to have each UILabel change color when long pressed, and without lifting their finger to end the long press, to change the value of the UILabel itself when the user pans up and down or side to side.
Right now, each UILabel has its own pan gesture method and long press gesture method. Is there any way to have a single long press/pan method for both UILabels but also have the ability to do something for one label and something else for another?
Also, is there a better approach to doing this? Eventually, I would also like to implement visual feedback when changing the value of the labels, such as in the form of animations.
I am new to iOS programming and programming in general and detailed answers are greatly appreciated. Thanks.
You can have a single function.No need to have seperate gestures for seperate Label.Example
//First add tag value to ur labels
label_one.tag=1;
label_two.tag=2;
UIPanGestureRecognizer * _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePanGesture:)];
_panGestureRecognizer.delegate = self;
[label_one addGestureRecognizer:_panGestureRecognizer];
UIPanGestureRecognizer * _panGestureRecognizer_two = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePanGesture:)];
_panGestureRecognizer_two.delegate = self;
[label_two addGestureRecognizer:_panGestureRecognizer_two];
-(void)handlePanGesture:(UIPanGestureRecognizer*)sender{
if(sender.tag==1){
}
else if(sender.tag==2){
}
}
Same goes for other gesture as well
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.
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.
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.
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.
Can you help me in how we can create a marquee for news , but I need that every new be clickable to show the detail page of the selected new.
I know we have many of examples considering marquee but no one was clickable for every item in the marquee.
Please help me
If you use something like this:
https://www.cocoacontrols.com/controls/marqueelabel
I'm assuming you should have NSArray of news. Put first news into marquee label as a text and start animating it.
You may be notified when marquee view reaches end scrolling (animating) then, put second news.
That's how you know which news user's currently viewing.
To be able to click on marquee and get notification:
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userDidTapOnNews:)];
[marquee addGestureRecognizer: tap];
-(void)userDidTapOnNews:(UITapGestureRecognizer *)gestureRecognizer
{
// Marquee tapped, your turn ...
// Use gestureRecognizer parameter if you need the view tapped
Marquee *marque = gestureRecognizer.View;
...
// Get news from your news `NSArray`
}
Hope it helps
I am trying to add a tap recognizer to a UILabel programmatically. It is not working... I have searched and tried a millions things of stackoverflow, but I can't get it to work...
Here is my current code:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(more:)];
[self.moreLabel setUserInteractionEnabled:YES];
[self.moreLabel addGestureRecognizer:tap];
- (void)more:(UITapGestureRecognizer *)sender {
NSLog(#"HIT?");
}
It will work if I add it to the top view but I don't want that ;) Any help is appreciated.
Solved using #Savitha comment:
Summary of answer: "The view I was trying to touch wasn't under another view so either using bringToFront or setting it correctly in IB was the answere."
Bring moreLabel to front.
[self.view bringViewToFront:self.moreLabel];
First off this question has been helpful in my understanding of how to subclass UIButton for long presses. I would like to do the same for UISegmentedControl, however I don't see how I would be able to identify which segment was held down since UISegmentedControl does allow direct access to it's segments (UISegmentedControl.h shows them as private). I could just customize a few UIButtons to look like an UISegmentedControl however I would also have to implement the momentary switch logic. Which wouldn't be a big deal but subclassing UISegmentedControl seems cleaner to me.
BTW, I'm using this control to imitate a radio's preset controls: tap to go to a saved station and hold to assign the current station to that segment.
I tried this without subclassing and it seems to work.
UILongPressGestureRecognizer* recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(pressGesture:)];
recognizer.delegate = self;
[mySegCtrl addGestureRecognizer:recognizer];
[recognizer release];
...
-(void)pressGesture:(UILongPressGestureRecognizer*)gesture
{
NSLog(#"pressGesture %#", gesture);
}
Long press first selects the segment then fires the gesture. If you aren't getting the callback check my code - I got stuck for a while because I wasn't setting recognizer.delegate=self.