I want to get rid of magnification and text selection in UITextView but I need phone number, link and address detectors. I am using
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
gestureRecognizer.enabled = NO;
}
[super addGestureRecognizer:gestureRecognizer];
return;}
to stop magnification, but it also stops selection phone number / address / link detected by textview.
If I do [_txtView setSelectable:NO]; it stops both magnification and text selection as well as data detection.
After quite a long time trying, I managed to stop text selection, magnifying, and keeping data detection (links clickable etc) by overriding addGestureRecognizer on a UITextView subclass allowing only UILongPressGestureRecognizer delaying touch ending:
UIUnselectableTextView.m
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && gestureRecognizer.delaysTouchesEnded)
{
[super addGestureRecognizer:gestureRecognizer];
}
}
Put image on your UITextview in .xib file then put below code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
UITapGestureRecognizer *tappress= [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(longPressed:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:tappress];
}
-(void)longPressed:(UILongPressGestureRecognizer *)sender
{
[yourtextview becomeFirstResponder];
}
in my code img is a UIImageview
Try this:
Set the delegate of the textview to your viewcontroller
add this method
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSRange selected;
selected.location = 0;
selected.length = 0;
textView.selectedRange = selected;
}
This would disable the magnification but still have links clickable
You just need to make sure you have set right parameters for the UItextfield (and there is no need to actually have it done by overriding the gestures). I guess if you change your attributes for "Behaviour" and "Detection" in interface builder as following you will have your desired behaviour.
Related
How can I do to perform some specific action (like showing a modal or pushing a controller) when user click on some formated/specific word in Uitextview (or UIlabel) ?
I've heard about NSAttributedString but I'm not sure how to make this with it.
What I want to have is the same results as the facebook app. When you click on a name it push another controller :
If you can give me some hint, tutorial or whatever you want please.
Add gesture recognizer to your UITextView:
//bind gesture
[_yourTextView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:delegate action:#selector(didReceiveGestureOnText:)]];
And then just check which word is clicked in didReceiveGestureOnText with following code:
+(NSString*)getPressedWordWithRecognizer:(UIGestureRecognizer*)recognizer
{
//get view
UITextView *textView = (UITextView *)recognizer.view;
//get location
CGPoint location = [recognizer locationInView:textView];
UITextPosition *tapPosition = [textView closestPositionToPoint:location];
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
//return string
return [textView textInRange:textRange];
}
EDIT
This is how your didReceiveGestureOnText method should look-like:
-(void)didReceiveGestureOnText:(UITapGestureRecognizer*)recognizer
{
//check if this is actual user
NSString* pressedWord = [delegate getPressedWordWithRecognizer:recognizer];
}
However this will led you in checking strings after all which is in really cool(as it's slow).
It's hackish, but you can try using TTTAttributedLabel and attach a custom URL to the word/phrase within the label:
TTTAttributedLabel *label;
//after setting the label text:
[label addLinkToURL:[NSURL URLWithString:#"http://www.stackoverflow.com"] withRange:[label.text rangeOfString:#"CLICKABLE TEXT HERE"]];
Then in the delegate method, you call your selected action:
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
// for handling the URL but we just call our action
[self userHasClickedTextInLabel];
}
You can add a gesture recognizer to the label. eg:
[yourLabel setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelButton:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[yourLabel addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
You didnt specify which version you are using or whether its via IB or programmatically. This sets up the gesture recognizer on your label. The selector is the action you want to carry out eg performSegue etc. Let me know how this goes
I am devolepeing an app where i am entering data to a textfield from custom view(not input view).Problem is that i don't want the system keypad to popup when i touch the textfield.I have gone through Apple text programming document but was not able to find the solution.How to achieve this.
You can also use gestures,
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard {
[self.view endEditing:YES];
}
try this code may help you
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==/**urs testField on which you want to remove keyboard on click*/ ) {
[textField resignFirstResponder];
//*its depend your requirement
[here you can call yours custom view];
}
return YES;
}
adjust as per req..
Set the userInteractionEnabled property:
//UITextField *text;
text.userInteractionEnabled = NO;
Not sure, what exactly you are expecting. I hope that you need to handle the UITextField delegates but without UIKeyboard : If so, then implement delegate of
-(void)textFieldDidBeginEditing:(UITextField *)sender{
// resign the textfield and do your stuff with the data
}
Assign a IBOutlet property to that particular UITextField
and then in viewDidLoad add the following code [self.YourUITextField setUserInteractionEnabled:NO];
Disable the editing for the particular field
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if ([textField isEqual:myTextField]) {
return NO;
}
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES]; // this statement will hide keyboard. Useful when we use many textfields based on tag value.
return YES;
}
How do I prevent the editing of text in a UITextField along with hiding the cursor/caret/magnifying glass, but still present the keyboard, as I am using an inputView with a UIPickerView/UIDatePickerView?
Setting userInteractionEnabled to NO does not work, because it no longer receives any touches and will not present the keyboard.
Subclass UITextField
//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}
//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
gestureRecognizer.enabled = NO;
}
[super addGestureRecognizer:gestureRecognizer];
}
In your UITextFieldDelegate
//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
Now just set your text programmatically from the result of your UIPickerView/UIDatePicker.
Hiding the cursor is a lot simpler in iOS 7. Still need some trickery to disable the loupe
textField.tintColor = [UIColor clearColor];
I hope this will helpful for you.
Set cursor UIColor -> Empty. In UI, it will be hidden.
[[self.textField valueForKey:#"textInputTraits"] setValue:[UIColor clearColor] forKey:#"insertionPointColor"];
I found the best solution was
- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:) || action == #selector(selectAll:) || action == #selector(paste:))
{
returnNO;
}
return [super canPerformAction:action withSender:sender];
}
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
To have a UITextField without interaction, but still work with an inputView:
Subclass UITextField with these methods:
// Hide the cursor
- (CGRect)caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
// All touches inside will be ignored
// and intercepted by the superview
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return NO;
}
The last method will single-handedly prevent any editing and the magnifier glass, as you won't be able to tap the UITextField.
This works great if you're using a textfield in a UITableViewCell for example, and then can toggle firstResponder status through tableView:didSelectRowAtIndexPath:.
To disable any interaction with textfield except for making it a first responder you can just place a UIButton of the same size right over the textfield. Code for button tap event could be something like this:
- (IBAction)btnEditPhoneTapped:(id)sender
{
if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
}
What I'm working with is I have a custom UIView that combines a UILabel with a UITextField as part of some user input. I have several of these throughout the view that is being displayed in the app. What would be good is to be able to have the user touch either the UILabel or the UITextField and then have the UITextField allow for input of the field.
Is there an easy way to do this?
Add a tap gesture recognizer to your label, and when tapped tell the corresponding textfield to become first responder (which brings up the keyboard).
// In your init or awakeFromNib:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped:)];
self.label.userInteractionEnabled = YES;
[self.label addGestureRecognizer:tap];
...
- (void)labelTapped:(id)sender
{
[self.textField becomeFirstResponder];
}
Yes, there is and I actually tested it. You can add a tap gesture recognizer to your view (the one that contains the label and the text view) like so:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)];
[self addGestureRecognizer:[tap autorelease]];
and then, inside viewTapped:
-(void)viewTapped:(id)sender {
[self.yourTextField becomeFirstResponder];
}
Hope this helps!
You can define UIGestureRecognizer delegate method and check that is the the required tap gesture area . If it is then return TRUE else return FALSE .
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == yourButton)) {//change it to your condition
return NO;
}
return YES;
}
I created a form and the keypad (Numeric only) appears when entering data like your age.
I want the keyboard to disappear when the user taps the background and I want to add a "Done" button in the empty slot under the 7 (next to the zero). (im using the Number Pad keyboard)
I found this example but I have a few questions.
In
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
[aTextField1 resignFirstResponder];
[aTextField2 resignFirstResponder];
[aTextField3 resignFirstResponder];
}
If I have more than 1 text field in my form.
Will I need to write every textfield in the dismissKeyboard method?
Easy way to do this is to use the method provided in UIView
- (BOOL) endEditing:(BOOL)force;
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
So just do this:
-(void)dismissKeyboard {
[self.view endEditing:YES];
}
and it will support any more text fields you add on your page (under that UIView of course)
You should only send dismissKeyboard to that textField that you are currently editing.
In your code you have got memory leak. Better use this one:
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
To check if UITextField is currently in edit mode you can check its property:
A Boolean value indicating whether the text field is currently in edit mode. (read-only)
#property(nonatomic, readonly, getter=isEditing) BOOL editing
For example, you have 3 text fields then dismissKeyboard will look something like this:
-(void)dismissKeyboard
{
UITextField *activeTextField = nil;
if ([textField1 isEditing]) activeTextField = textField1;
else if ([textField2 isEditing]) activeTextField = textField2;
else if ([textField3 isEditing]) activeTextField = textField3;
if (activeTextField) [activeTextField resignFirstResponder];
}
I use the same functionality in many of my apps. Rather than using the GestureRecognizer, I set my view up as a UIControl, rather than a UIView. You can still do the things you'd do with a UIView, but you can also assign IBActions to be performed when interacting with the view.
Here's how to do it:
In Interface Builder, select your view. Then, assign its class to UIControl. (It's probably set up as UIView currently.
In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:
- (IBAction)backgroundTap:(id)sender
{
if ([textField1 isEditing]) {
[textField1 resignFirstResponder];
} else if ([textField2 isEditing]) {
[textField2 resignFirstResponder];
}
}
Finally, in Interface Builder, connect the IBAction you created to the UIControl.
Read this article, it may help you
Writing iOS 4 Code to Hide the iPhone Keyboard (Xcode 4)
Here i give common text field object. and asign reference to it in "textFieldShouldBeginEditing" method. that will common for all text field..
In this case, you need to dismiss one text field that will hide keyboard..
declare textfield object in .h file.
#interface RootViewController : UIViewController
{
IBOutlet UITextField* txt_common;
}
IN .m file
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
-(void)dismissKeyboard
{
NSLog(#"hi");
[txt_common resignFirstResponder];
}
#pragma mark TextField methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"hi++++++++++++++++++");
txt_common=textField;
return YES;
}