I am trying to make a UILabel that if I click I can edit it. I have a hidden textfield.
Per this answer: How to customize UILabel clickable
I tried the following:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tapInput = [[UIGestureRecognizer alloc] initWithTarget:self action:#selector(didTapLabel)];
[lblInput setUserInteractionEnabled:YES];
[lblInput addGestureRecognizer:tapInput];
}
-(void)didTapLabel
{
[txtHidden becomeFirstResponder];
}
But my break point inside didTapLabel is not firing. Everything is hooked up correctly on the storyboard.
Use this code, It's worked. Tested.
UILabel * clickMeLbl=[[UILabel alloc] initWithFrame:CGRectMake(75,30,170,30)];
clickMeLbl.text = #"Click Me";
clickMeLbl.font = [UIFont fontWithName:#"Helvetica-Bold" size: 18.0f ];
clickMeLbl.textAlignment = NSTextAlignmentCenter;
clickMeLbl.textColor = [UIColor blackColor];
[self.view addSubview:clickMeLbl];
clickMeLbl.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(didTapLabelWithGesture:)];
[clickMeLbl addGestureRecognizer:tapGesture];
- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
NSLog(#"Click Me Label Clicked");
}
Try enabling user interaction here for label,Worked for me, and please use UITapGesture instead of UIGesture
And Textfield should be below uilabel because if the textfield is above label then uilabel's user interaction won't be considered
I added tapGesture for self.view and UILabel (subview of mainView), each performs different selectors.
But the only main view tapGesture is being called and label tapgesture is not being called. How it is handled?
Here is the code:
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[lbl addGestureRecognizer:tapLblClick];
For two selectors only one method is called tapGesForSelf.
Here lbl is the subview of self.view.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
[_lbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[_lbl addGestureRecognizer:tapLblClick];
[tapGesForSelf requireGestureRecognizerToFail:tapLblClick];
}
- (void)tapGesForSelf:(UITapGestureRecognizer *)gesture
{
NSLog(#"self");
}
- (void)tapGesForLbl:(UITapGestureRecognizer *)gesture
{
NSLog(#"label");
}
I post answer for your question now once I tried and it worked well.
First see the label in design.I set label text as "Tap Me"
Now I set the code for view and label
- (void)viewDidLoad
{
[super viewDidLoad];
//TapGesture for Label
UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapLabel:)];
tapLabel.delegate = self;
tapLabel.numberOfTapsRequired = 1;
lblTapMe.userInteractionEnabled = YES;
[lblTapMe addGestureRecognizer:tapLabel];
//TapGesture for View
UITapGestureRecognizer *tapMainView = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapMainView:)];
tapMainView.delegate = self;
tapMainView.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapMainView];
}
//Action method for Label
-(void)actionTapLabel:(UITapGestureRecognizer *)gestureOnLabel{
UILabel *label = (UILabel *)gestureOnLabel.view;
NSLog(#"Lable text is - %#",label.text);
}
//Action method for View
-(void)actionTapMainView:(UITapGestureRecognizer *)gestureOnMainView{
NSLog(#"The Main view is tapped");
}
Output Screenshot
Please setUserInteractionEnabled:YES for interact the TapGesture
[lbl setUserInteractionEnabled:YES];
I have a UITextView which becomes first responder by tapping. The code looks like this.
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(editTextRecognizerTabbed:)];
recognizer.delegate = self;
recognizer.numberOfTapsRequired = 1;
[self.textViewNotes addGestureRecognizer:recognizer];
- (void) editTextRecognizerTabbed:(UITapGestureRecognizer *) aRecognizer;
{
self.textViewNotes.dataDetectorTypes = UIDataDetectorTypeNone;
self.textViewNotes.editable = YES;
[self.textViewNotes becomeFirstResponder];
}
So when the user taps on the textview, the textview becomes the first-responder and the focus is on the end of the text. I want place the focus on the tapped position in the Text.
Any idea?
This question already has answers here:
iOS - Dismiss keyboard when touching outside of UITextField
(38 answers)
Closed 8 years ago.
I have one TableViewCell that has UITextField in it.
I want when click out of UITextField hidden keyboard but I don't about that.
this is my code:
#implementation TextFieldCell
#synthesize Textfield,placeholder;
- (void)awakeFromNib
{
// Initialization code
Textfield = [self makeTextField];
[self addSubview:Textfield];
placeholder = [self PlaceHolderLabel];
[self addSubview:placeholder];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (UITextField *)makeTextField
{
CGRect TextFrame = CGRectMake(0,0,300,80);
UITextField *textfield = [[UITextField alloc]initWithFrame:TextFrame];
textfield.delegate = self;
textfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
textfield.backgroundColor = [UIColor yellowColor];
textfield.placeholder = [NSString stringWithFormat:#"Mamal"];
UIView *spacerleftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
UIView *spacerrightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
[textfield setLeftViewMode:UITextFieldViewModeAlways];
[textfield setRightViewMode:UITextFieldViewModeAlways];
textfield.leftView = spacerleftView;
textfield.rightView = spacerrightView;
return textfield;
}
- (UILabel *)PlaceHolderLabel{
NSString *labelText = Textfield.placeholder;
Textfield.placeholder = nil;
CGRect labelFrame = CGRectMake(0,0,280,80);
UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
[placeholderLabel setTextColor:[UIColor lightGrayColor]];
[placeholderLabel setText:labelText];
placeholderLabel.textAlignment = NSTextAlignmentRight;
return placeholderLabel;
}
- (UILabel *)clearPlaceHolderLabel{
NSString *labelText = nil;
CGRect labelFrame = CGRectMake(0,0,280,80);
UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
[placeholderLabel setText:labelText];
return placeholderLabel;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
//perform actions.
NSLog(#"start");
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"end");
}
Add gesture recogniser in viewDidLoad :
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeKeyboard:)];
[self.view addGestureRecognizer:tap];
and then implement closeKeyboard:
- (IBAction)closeKeyboard:(id)sender {
[self.view endEditing:YES];}
my friend I had this problem but I can solve that.
you don't need add UITextField in TableViewCell , you can create TextField in ViewController or TableViewController that your tableView is inner it and then add TextField inside any cell that you want.
like this code:
ViewController.h
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
#property (nonatomic,strong) UITableView *table;
#property (weak,nonatomic) UITextField *Textfield;
ViewController.m
#implementation ViewController
#synthesize table,Textfield;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//add table in view
table = [self makeTableView];
[self.view addSubview:table];
[self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct
//run textfield programmatically
Textfield = [self makeTextField];
//hide keyboard with hideKeyboard selector
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.table addGestureRecognizer:gestureRecognizer];
}
- (void) hideKeyboard {
[Textfield resignFirstResponder];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
Try to use
[self.view setEditing:YES];
in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
try this:
// somewhere in viewDidLoad
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnBackground)];
[self.view addGestureRecognizer:tapRecognizer];
self.view.userInteractionEnabled = YES;
// ...
- (void)tapOnBackground
{
[self.view endEditing:YES];
}
Add this code in your textField implementation method.
//Set to Remove Keyboard from view
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
After that create dismissKeyboard method.
//First Responder Remove Keyboard
-(void)dismissKeyboard {
[Textfield resignFirstResponder];
}
I'm trying to make a UITextView that detects links AND becomes editable when a tap doesn't fall on a link.
The native iphone notes app exhibits this behavior, as does simplenote.
The closest solution I have found is outlined here: http://blog.stevex.net/2012/05/editable-uitextview-with-links/ Using this code:
// Add the tap gesture recogniser to the view.
- (void)configureTextView {
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(textViewTapped:)];
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
[textView addGestureRecognizer:recognizer];
}
// Notification from the recogniser that the UITextView was tapped
- (void)textViewTapped:(UIGestureRecognizer *)recognizer {
UITextView *textView = (UITextView *)recognizer.view;
textView.editable = YES;
[textView becomeFirstResponder];
}
// UITextViewDelegate method
- (void)textViewDidEndEditing:(UITextView *)textView {
textView.editable = NO;
}
The problem with this solution is that it requires a long press to follow a link.