I want to change the text of a label, then have the user move it to where they want it on the screen (which is currently working) (the user hits - "Add text").
Once they place it where they would like. I want the "Add text" button to create a new label that the user can move. I'm not sure how to create these on the fly an to make sure that the gesture recognizers function with the new label. Thanks for suggestions.
This is what I have now,,, doesn't work quite yet.
-(IBAction)addText:(id)sender
{
textView.hidden=YES;
labelShirt.text= textField.text;
[textField resignFirstResponder];
[self addTextButtonPressed];
}
-(void)addTextButtonPressed
{
// CGRect *textFrame =
// myInitialFrame is a CGRect you choose to place your label
UILabel *myNewLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,50,100,100)];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(labelMoved:)];
myNewLabel.text =textField.text;
[self.view addSubview:myNewLabel];
}
-(void)labelMoved:(UIPanGestureRecognizer *)sender
{
CGPoint translation = [sender translationInView:self.view];
sender.view.frame = CGRectOffset(sender.view.frame, translation.x, translation.y);
}
// The action that is added to your add text button
-(void)addTextButtonPressed
{
// myInitialFrame is a CGRect you choose to place your label
UILabel *myNewLabel = [[UILabel alloc] initWithFrame:myInitialFrame];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(labelMoved:)];
myNewLabel.text = #"My initial text";
// EDIT
[self.view addSubview:myNewLabel];
[myNewLabel addGestureRecognizer:panGestureRecognizer];
}
-(void)labelMoved:(UIPanGestureRecognizer *)sender
{
CGPoint translation = [sender translationInView:self.view];
sender.view.frame = CGRectOffset(sender.view.frame, translation.x, translation.y);
}
I don't know if that's enough to solve your problem, just comment if you still need more explanation.
Related
I have a NSAttributedString like so:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"testing it out #clickhere"];
NSInteger length = str.length;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)];
The NSMutableAttributedString gets set to a UILabel like so:
label.attributedText = str;
How do I make a tap gesture (or something clickable) to another viewcontroller for the words '#clickhere in the string above?
Thanks!
I think, the best way is adding the UIGestureRecognizer to your UILabel and validate the frame that you would like.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[_yourLabel addGestureRecognizer:singleTap];
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _yourLabel];
//Modify the validFrame that you would like to enable the touch
//or get the frame from _yourLabel using the NSMutableAttributedString, if possible
CGRect validFrame = CGRectMake(0, 0, 300, 44);
if(YES == CGRectContainsPoint(validFrame, touchPoint)
{
//Handle here.
}
}
Simply first add a gesture to your label
[label setUserInteractionEnabled:YES];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[label addGestureRecognizer:gesture];
control your gesture area in the below method
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
static CGRect touchableRect = CGRectMake(100.0f, 0.0f, 100.0f, 50.0f); // Give your rect as you need.
CGPoint touchPoint = [gestureRecognizer locationInView:self.view];
if (CGRectContainsPoint(touchableRect, touchPoint))
{
//User has tap on where you want. Do your other stuff here
}
}
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
Tap.numberOfTapsRequired = 1;
// label Name Is your Label Name
[labelName addGestureRecognizer:Tap];
-(void)tapDetected
{
//your code
}
I'd just like to add on Ramshad's answer, about how to deal with the valid frame.
For this you might want to consider using UITextView instead of UILabel, which doesn't give you access to how it manages layout the text. By disabling editing, selection and scrolling, UITextView behaves roughly the same as a UILabel, except some padding you have to remove.
For convenience, you might want to add a little category to UITextView, in which you write a method to test if a point touches any of the characters in range.
- (BOOL)point:(CGPoint)point touchesSomeCharacterInRange:(NSRange)range
{
NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:range actualCharacterRange:NULL];
BOOL touches = NO;
for (NSUInteger index = glyphRange.location; index < glyphRange.location + glyphRange.length; index++) {
CGRect rectForGlyphInContainer = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(index, 1) inTextContainer:self.textContainer];
CGRect rectForGlyphInTextView = CGRectOffset(rectForGlyphInContainer, self.textContainerInset.left, self.textContainerInset.top);
if (CGRectContainsPoint(rectForGlyphInTextView, point)) {
touches = YES;
break;
}
}
return touches;
}
This would also work for a fragment of text containing multiple words spreading across multiple lines due to word wrap. It would also work on localized texts as we deal with glyphs that are printed.
The idea is the same as the accepted answer. Here is the way in Swift.
Suppose you have set up your label ready.
youLabel.text = "please tab me!"
Add tapGestuerRecognizer to your label
let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
yourLabel.addGestureRecognizer(tap)
Add the String extension method to String to calculate the string rect.
extension String {
func rect(font: UIFont) -> CGRect {
let label = UILabel(frame: .zero)
label.font = font
label.text = self
label.sizeToFit()
return label.frame
}
}
Calculate the available tap rect of the tap gesture.
let pleaseStringRect = "please".rect(font: yourFont)
let tapStringRect = "tap".rect(font: yourFont)
let tapAreaRect = CGRect(x: pleaseStringRect.width, y: tapStringRect.origin.y, width: tapStringRect.width, height: tapStringRect.height"
In the action do what you want
#objc private func tapAction(tap: UITapGestureRecognizer) {
let position = tap.location(in: content)
guard reporterFirstNameRect.contains(position) else {
return
}
// Do the tap action stuff
}
That's it, happy coding.
I Created a sample application, when we click on a button another view(ResizingViewController) view shows up.
when i drag ResizingViewController it is moving correctly but i can see another view behind it.I think it is super view (as show in in this image, but not able to make both move at same time).
please help me to fix it?(if i drag view then both should move to same position)
my code is as below
- (IBAction)click_Action:(id)sender {
if (!resizingViewController) {
return;
}
//Creating Right Button, asigning a method to it.
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Close"
style:UIBarButtonItemStylePlain target:self action:#selector(done)];
[resizingViewController.navigationItem setRightBarButtonItem:rightButton];
theNavigationController = [[UINavigationController alloc]
initWithRootViewController:resizingViewController];
[theNavigationController.navigationBar setTintColor:[UIColor blueColor]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
theNavigationController.modalPresentationStyle
=UIModalPresentationFormSheet;
}
[self presentViewController:theNavigationController animated:YES completion:nil];
CGRect r = CGRectMake(100, 100, 300, 400);
r = [self.view convertRect:r toView:self.view];
theNavigationController.view.superview.frame = r;
//Adding gesture reconizer to resizingviewcontroller
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanFrom:)];
[theNavigationController.view addGestureRecognizer:panGestureRecognizer];
}
-(void)handlePanFrom:(UIPanGestureRecognizer *) recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
//Changing Center of the the view as soon as user selects the view and drags
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
CGPoint r = CGPointMake(recognizer.view.center.x+100,
recognizer.view.center.y+100);
r = [self.view convertPoint:r toView:self.view];
recognizer.view.superview.center = r;
//Should not forget to give below code, else view goes out of bounds.
[recognizer setTranslation:CGPointZero inView:self.view];
}
OK i fixed this.(marking this as answer so that if any one have same problem can use this)
-(void)handlePanFrom:(UIPanGestureRecognizer *) recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
recognizer.view.superview.center =
CGPointMake(recognizer.view.superview.center.x+translation.x,
recognizer.view.superview.center.y+translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
}
I have a UIScrollView and inside this I have UILabels. I need to detect touch events for the UILabels. At the moment, it is detecting the touch inside the second label only. It ignores the first.
I have the code -
Creating the UIScrollView
backGroundView = [[UIScrollView alloc] init];
backGroundView.frame= self.view.frame;
backGroundView.userInteractionEnabled = YES;
[backGroundView setScrollEnabled:YES];
backGroundView.showsVerticalScrollIndicator = YES;
backGroundView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
backGroundView.delegate = self;
[self.view addSubview:backGroundView];
Creating the UILabel
UILabel *OneDay = [[UILabel alloc] initWithFrame:CGRectMake(15, stockChart.bounds.origin.y + stockChart.bounds.size.height + 35, 40, 30)];
OneDay.text = #"1d";
OneDay.tag = 1;
OneDay.userInteractionEnabled = YES;
OneDay.layer.borderColor = [UIColor grayColor].CGColor;
OneDay.layer.borderWidth = 1.0f;
OneDay.textAlignment = UITextAlignmentCenter;
[OneDay addGestureRecognizer:detectTimeFrameChange];
[backGroundView addSubview:OneDay];
UILabel *FiveDay = [[UILabel alloc] initWithFrame:CGRectMake(45, stockChart.bounds.origin.y + stockChart.bounds.size.height + 35, 40, 30)];
FiveDay.text = #"5d";
FiveDay.tag = 2;
FiveDay.userInteractionEnabled = YES;
FiveDay.layer.borderColor = [UIColor grayColor].CGColor;
FiveDay.layer.borderWidth = 1.0f;
FiveDay.textAlignment = UITextAlignmentCenter;
[FiveDay addGestureRecognizer:detectTimeFrameChange];
[backGroundView addSubview:FiveDay];
Creating the gesturerecognizer
UITapGestureRecognizer *detectTimeFrameChange = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(timeFrameLabelTapped:)];
detectTimeFrameChange.numberOfTapsRequired = 1;
[backGroundView addGestureRecognizer:detectTimeFrameChange];
Handling gesture
-(void)timeFrameLabelTapped:(UITapGestureRecognizer*)recognizer{
if (recognizer.view.tag == 1) {
NSLog(#"One pressed");
}
else if (recognizer.view.tag == 2){
NSLog(#"2 pressed");
}
}
You can use this :
UITapGestureRecognizer *labelTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(labelTapped)];
labelTap.numberOfTapsRequired=1;
[yourLabel addGestureRecognizer:labelTap];
handle the touch tap event inside labelTapped method:
-(void)labelTapped
{
//your code to handle tap
}
Touch events are not detected on UIScrollview for getting your requirement add tap gestures to your label.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured)];
[OneDay addGestureRecognizer:singleTap];
-(void)singleTapGestureCaptured{
NSLog(#"touch detected");
}
You can find using tapgesturerecogniser like that...
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.scrollview addGestureRecognizer:singleFingerTap];
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if(recognizer.view.tag == 1){}
//Do stuff here...
}
Where have you written the touchesBegan?
If you want to detect the touches in the label you'll have to create a subclass of label and write your touchesBegan there to detect the touch events
The problem here is that you are trying to use the same gesture recognizer for multiple views. A gesture recognizer can only be attached to a single view at once. You are only receiving events from the last view, because that is the view the recognizer is currently attached to. To fix the issue, simply create a gesture recognizer for each view you want to detect touches in.
I was trying to create a label that should be draggable. But the dragged event is not firing (checked with break point). Below is the code.
- (IBAction)InsertText:(UIBarButtonItem *)sender {
UILabel *lblWatermark = [[UILabel alloc] initWithFrame:currentImage.frame];
lblWatermark.text = #"Copyright";
lblWatermark.userInteractionEnabled = YES;
[lblWatermark sizeToFit];
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[lblWatermark addGestureRecognizer:gesture];
[currentImage addSubview:lblWatermark];
}
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
// move label
label.center = CGPointMake(label.center.x + translation.x,
label.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:label];
}
Thank you in advance,
Spotted the error. Actually I had also to make
userInteractionEnabled = YES
Not only for the label But also for the Imageview in which I am adding the label.
Make sure your ViewController is the delegate for UIGestureRecognizer with (.h file):
#interface View : UIView <UIGestureRecognizerDelegate>
And then set:
gesture.delegate = self;
I'm a noob on IOS. I have searched a lot, but I didn't find any good tutorial about Drag and Drop on IOS. I just read that is not directly supported. Is that possible to Drag an item from a scroll view to a view, and pass some information with it? Imagine the MAIL app. I want to drag an email to the big view on the right, and pass some information with it. Is there any book, or tutorial, that can teach me how to make it?
TKS!
It's just a matter of using Gesture Recognizers (see Event Handler Guide).
Actual implementations depends a little upon how you want to do it. Here's a random example where I've got a split view control and I'm dragging something from the tableview on the left into the view on the right, the whole drag and drop triggered by a long press (e.g. a "tap and hold"). Thus, I just create a gesture recognizer in the viewDidLoad of the tableview controller (in my case, the master view controller):
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.tableView addGestureRecognizer:longPress];
And then I defined a gesture recognizer handler that implements the drag and drop, e.g.,
- (IBAction)longPress:(UIGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
// figure out which item in the table was selected
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[sender locationInView:self.tableView]];
if (!indexPath)
{
inDrag = NO;
return;
}
inDrag = YES;
// get the text of the item to be dragged
NSString *text = [NSString stringWithString:[[_objects objectAtIndex:indexPath.row] description]];
// create item to be dragged, in this example, just a simple UILabel
UIView *splitView = self.splitViewController.view;
CGPoint point = [sender locationInView:splitView];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [text sizeWithFont:font];
CGRect frame = CGRectMake(point.x - (size.width / 2.0), point.y - (size.height / 2.0), size.width, size.height);
draggedView = [[UILabel alloc] initWithFrame:frame];
[draggedView setFont:font];
[draggedView setText:text];
[draggedView setBackgroundColor:[UIColor clearColor]];
// now add the item to the view
[splitView addSubview:draggedView];
}
else if (sender.state == UIGestureRecognizerStateChanged && inDrag)
{
// we dragged it, so let's update the coordinates of the dragged view
UIView *splitView = self.splitViewController.view;
CGPoint point = [sender locationInView:splitView];
draggedView.center = point;
}
else if (sender.state == UIGestureRecognizerStateEnded && inDrag)
{
// we dropped, so remove it from the view
[draggedView removeFromSuperview];
// and let's figure out where we dropped it
UIView *detailView = self.detailViewController.view;
CGPoint point = [sender locationInView:detailView];
UIAlertView *alert;
if (CGRectContainsPoint(detailView.bounds, point))
alert = [[UIAlertView alloc] initWithTitle:#"dropped in details view" message:nil delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
else
alert = [[UIAlertView alloc] initWithTitle:#"dropped outside details view" message:nil delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
}
Clearly, if you're dragging a subview from your scrollview, you'd replace the indexPathForRowAtPoint logic with something like:
UIView *objectToDrag = nil;
CGPoint point = [sender locationInView:myView];
for (UIView *control in myView.subviews)
if (CGRectContainsPoint(control.frame, point))
objectToDrag = control;
if (!objectToDrag)
{
inDrag = NO;
return;
}
That would help you identify what you want to drag, but from there, the logic is very similar (except, rather than dragging the UILabel as in my example, you'd drag your objectToDrag).