Text View has to Detect links automatically in iOS - ios

I have a textView in which I type a url address and press a button. There is a service which detects the link and display links information like image, description etc,
My question is, do we have any option that without pressing a button can the service be automatically called.
For Example I am typing "facebook.com" now with out pressing a button can i call my web service

If you are using a UITextField then use do this to detect text change;
[textField addTarget:self
action:#selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
This is the method:
-(void)textFieldDidChange :(UITextField *)aTextField{
NSLog( #"text changed: %#", aTextField.text);
//Call your web Service
}
For UITextView implement its delegate methods:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//text is the new part that is added to textView
return YES;
}
Note: Don't forget to add the delegate.

Related

iOS UITextField resignFirstResponder issue

In my app, on registration screen, I have tableView which containes UITextField in each cell and Submit button in a footer. Everything works fine except when I enter value in the last TextField and hit submit button (Without resigning a keyboard). My function does not show a value because value is only entered when text editing is ended. I am using Delegate to get a value.
My code is:
In customcell.m:
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([textField.text length] >0) {
[self.cellImageview setImage:editImage];
}
else{
[self.cellImageview setImage:defaultImage];
}
[_delegate signUpTableCell:self didUpdateValue:textField.text inTextField:textField];
}
In my viewController:
-(void)signUpTableCell:(SignUpTableCell *)cell didUpdateValue:(NSString *)value inTextField:(UITextField *)textField {
NSLog(#"cell:%# value: %# textfield value:%# ", cell, value, textField.text);
[registerDictionary setValue:value forKey:textField.placeholder];
NSLog(#"registerDictionary:%#", registerDictionary);
}
Problem:
Last textField value is nil when I click on submit button, probably because
-(void)signUpTableCell:(SignUpTableCell *)cell didUpdateValue:(NSString *)value inTextField:(UITextField *)textField
does not get a call unless textField ends editing. If I click in empty place and resign a keyboard it works just fine. Is there a work around this problem?
Did you try to manually call resignFirstResponder() when clicking on the button?
Add below code to in submit button :
[self.view endEditing:yes];
or use
[self.yourTableview.scrollView endEditing:yes];
Fixed it by making few changes in my code. I had used [self.view endEditing:yes]; after I get values from registerDictionary . Stupid of me.

iOS toggle default keyboard from ABC mode to 123 mode via code?

I can see how to set the keyboard's overall type via:
self.myTextView.keyboardType = UIKeyboardTypeDefault;
How can I toggle the default keyboard's mode from ABC to 123 and back again via code? Basically the moment the user taps the # character (the # symbol is available when they're in 123 mode) I want to switch their keyboard back to ABC mode.
Any ideas?
You might be able to accomplish this in by using the UITextViewDelegate. It allows you to intercept the keys as they are pressed in the UITextView. This will allow you to switch keyboards when the user presses a certain key. In order to revert back to the default state of the UIKeyboardTypeDefault keyboard, you'll need to change the keyboard type to another, then back to the default.
In your ViewController.h file, make it implement the UITextViewDelegate protocol:
#interface ViewController : UIViewController <UITextViewDelegate>
In your ViewController's viewDidLoad method in the ViewController.m, set the textField's delegate to the view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTextView.delegate = self;
}
Finally, we need to capture the key as it is being entered and change the keyboard appropriately. We do this in the shouldChangeCharactersInRange: method.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if( [#"#" isEqualToString:text] )
{
NSLog( #"Toggling keyboard type");
textView.inputView = nil;
[textView resignFirstResponder];
[textView setKeyboardType:UIKeyboardTypeEmailAddress];
[textView becomeFirstResponder];
[textView reloadInputViews];
textView.inputView = nil;
[textView resignFirstResponder];
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder];
[textView reloadInputViews];
}
return YES;
}
So I was originally confused and thought you actually wanted to change the keyboard type. Now that I'm reading your comment, I realize that you're actually wanting to reset the keyboard to it's default state of showing the letters, instead of numbers and special characters. The above code now does that.

How can I get the Edit Menu to appear in a UIWebView with contentEditable?

I have an iPad App with an embedded Text Editor. The editor is a UIWebView with contenteditable set to true. The form also contains a simple text field for the title of the document.
If the user types in the title, then taps Return they can enter text in WebView, and tap-and-hold to get the magnifying glass and then the edit menu.
If, however, the user enters the title, and then taps on the web view they can enter text, tap-and-hold to get the magnifying glass, but the edit menu never appears.
The only difference I can see is that in the first case the textFieldShouldReturn: method fires first, and then the textFieldDidEndEditing: method fires, while in the second case only the textFieldDidEndEditing: method fires.
Here are the two methods in question:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
and
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *js = [NSString stringWithFormat:#"document.getElementById('theBody').setAttribute('contenteditable','true')"];
[self.webView stringByEvaluatingJavaScriptFromString:js];
[self.webView becomeFirstResponder];
[self.webView stringByEvaluatingJavaScriptFromString: #"document.getElementsByTagName('body')[0].focus()"];
}
Does anyone have any idea what the difference is, and how I can get the edit menu to appear?
I believe this is an iOS 7 bug, try adding the following code to your view controller as a workaround.
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
//Additional inicialization code.
}
Note that property is available on iOS 7 or later.
Hope this helps!

Capture text from UITextField in instance variable when another UIButton is pressed on UIView

I have a few UITextFields (within UITableViewCells) on my UIView with a "Save" UIButton. I want to do some basic validation on the UITextFields when the user clicks the "Save" button.
I have overridden textFieldDidEndEditing to save each of my UITextField data to an instance variable; however, if a user clicks the save button before either clicking the "Return" button of the UIKeyboard or clicking on another UITextField the data in my last UITextField is never saved to my instance variable and validation always fails.
I am looking for a way to trigger an "onBlur" (I know that's a JS thing)-type event to save my string in UITextField to my instance variable.
I've looked through the UITextFieldDelegate Protocol and I do not see anything like this.
Is there a method I may be missing?
to trigger textFieldDidEndEditing on your UITextField, you will need to call
[_txt resignFirstResponder];
were _txt is your UITextField
Please note that if you dont have a reference to _txt and you need to find the first responder in order to resign it
You could use the solution from this question Get the current first responder without using a private API
Then instead of calling
[_txt resignFirstResponder];
you would call
[self.view findAndResignFirstResponder];
Try this
// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:#"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
which will trigger
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
// Call webservice
return YES;
}

How can i hide the keyboard when the uitextView is in focus~~

I hava a UITextView ,and after some characters' input, I want to use a button click to hide the keyboard and the UITextView is still in focus....[Attention:the button is not a return key or a done key];
Have you set delegate to your UITextView instance?
If so, try using [_textView resignFirstResponder];
else
Try using [self.view endEditing:YES];
mplement following code to your view controller .m file & .h file add delegate of textView
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}
Now goto interface builder, select your textview & set return key type done.

Resources