I have looked all over for an answer to this but essentially what I am trying to do is when a person pressed the colon key on their iphones keyboard I want to be notified and perform a certain action. I hope this makes sense. If you do offer an answer keep in mind I am a relatively new IOS developer :)
Thanks!
edit: Incase my above statement didn't quite make sense this is what will happen ideally:
user taps on textfield
user presses the number 1 key
notification is sent that user pressed the number 1 key
instead of the number 1 printed, the text will be replaced with the number 2.
this is a simple example.
Here's an example of a delegate method for a UITextField where if the user tries to enter an uppercase character it will appear as a lowercase character instead:
-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSString* lc = [string lowercaseString];
if ([string isEqualToString:lc])
return YES;
textField.text =
[textField.text stringByReplacingCharactersInRange:range
withString:lc];
return NO;
}
You should be able to do something similar for your particular use case.
As mentioned before, use this callback and change in there:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//check here if the new character is the one you are looking for
if ([string isEqualToString:#"a"])
{
//create a new string with the character you want to use instead
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:#"A"];
//set it as the text for your text field
[textField setText:newText];
return NO;
}
return YES;
}
Related
I've a UITextField with text like, +91- (that's country caller code of India). Now when my user input his number into that textfield, it would look like this, +91-1234567890 that's good, now when he tap (x) delete key from keyboard, I want to restrict deletion, its only possible up to, 1 (first digit of his mobile number), at any case, he should not be able to delete +91-. I'm able to do it with - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; delegate, like this,
1) First way:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#""]) { //detect back space
if([textField.text hasSuffix:#"-"]) { //has suffix `-`
return NO;
}
}
}
2) Second way:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//if text length is length of caller code and detect back space
if(textField.text.length<=4 && [string isEqualToString:#""]) {
return NO;
}
return YES;
}
In both the ways, I'm getting what I want, but not sure its proper or not? Any more smoother way?
why you not try just like the simple method add the one more UIView in prefix of the UItextfield
You can simply show a non-editable UILabel before the UITextField where the user actually enters the number. When you get the input from the user and want to process it, prefix "+91-" before the user input string
I am making a simple application that has a TextField and a TextView
Whatever we write in textfield will get updated in textview (Both are in a single View, and it is the Root view).
the textfield will get updated every time i hit any key in the keyboard.
I know that the delegate method "textfielddidEditing" helps , but it only helps when we click on the field or click back. I want the method that invokes every time when i hit anything in the keyboard.
Any help is appreciated.
I'm still learning iOS
Please use below textfield delegate method. Its easy and simple to use with all appen and delete mechanism. You can delete multiple characters too by selection. Just store newString value to your textView
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSMutableString *newString = [NSMutableString stringWithString:textField.text];
if (range.length > 0) {
[newString replaceCharactersInRange:range withString:string];
}
else {
[newString appendString:string];
}
NSLog(#"%#",newString);
return YES;
}
You can use UITextField Delegate method. It invokes every time when you hit anything in the keyboard. Write your code as per your requirement..
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length>0)
{
self.yourtextview.text=textField.text;
}
return YES;
}
Hope it helps you..
The above code mentioned by Vidhyanad900 will work. But its if would fail in case a user deletes the text. (Hits backspace or selects complete text and deletes it)
Hence Modifying the code
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int iNewLength = [textField length];
if([text isEqualToString:#""]) //If text is deleted
{
if([textField selectedTextRange]) //If Text is Seleceted And Deleted
iNewLength = [textField length] - [textField selectedRange].length;
else
iNewLength = [textField length] - 1;
}
if (iNewLength > 0)
{
self.yourtextview.text=textField.text;
}
return YES;
}
I want to set a max length to my UITexField but this is inserted in a UIAlertView,
I'm trying to find out the right information to solve this problem,on this beautiful site but there is nothing.
Could someone help me to this?
I think this is a better implementation of the UITextFieldDelegate method, as it will not remove the text end in front of the users eyes, it just prevents the user from typing in more text.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (newString.length > MAXLENGTH)
{
return NO;
}
return YES;
}
Don't forget to implement the UITextFieldDelegate protocol, and set your controller as the delegate of the text field (see Anoop Vaidya answer)
As you can access the alertView's textField by
UITextField *theTitleTextField = [alertView valueForKey:#"_bodyTextLabel"];
If you want to find your's textfield added, then use textFieldAtIndex: as suggested by Martin R.
Then implement UITextFieldDelegate protocol.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > MAXLENGTH) {
textField.text = [textField.text substringToIndex:MAXLENGTH-1];
return NO;
}
return YES;
}
Is there a way to get notifications of when the user presses the backspace key on a UITextField, even if the field is empty?
I would like to be able to trigger some code when the user backspaces on an empty field.
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
This is how you get notified on every key the user presses. Then you have to figure out (i think in the notification is a property for it) which key was pressed.
Or you can use the textfield delegate function
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
To figure out what key was pressed
I can think of a technique to get you what you want but its going to take a bit of code. In essence, always keep a single Unicode "space" character at the front of the string. I don't have my book with me, but there is a really thin space character you can use.
You will put most of your code here:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
when the field begins editing, prefix any string there with the space
when the user appends text as seen in shouldChangeCharactersInRange, append it.
if the user tries to delete the final last character, or a range that contains it, then return NO, make the change, then add back the leading space char.
when the textField is finished editing, remove the leading space
Since you need to detect not when text was deleted but when the backspace key is pressed, you need to do quite a bit more than implement UITextFieldDelegate.
Read this blog post about how UITextField forwards all the UIKeyInput methods to a private class UIFieldEditor. The writer dynamically subclasses UIFieldEditor at runtime in order to detect these events.
Hope this points you in the right direction!
This will detect backspace. Now you can do whatever when backspace is pressed.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField==txtMobileNo)
{
const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
int isBackSpace = strcmp(_char, "\b");
if (isBackSpace == -8) {
NSLog(#"isBackSpace");
if (textField.text.length == 9)
{
}
return YES; // is backspace
}
else if (textField.text.length == 10) {
return YES;
}
}
return NO;
}
I am trying to figure out if there is a way to implement an autocomplete functionality in a UITextField for specific values.
I know that the UITextField can do this using the iPhone dictionary (much like searching google in safari, etc), but I want to be able to programmatically have it correct to certain values that I specify.
How to do this?
I did something very similar to this while working on a recent and rather large project. We had a constantly changing list of auto complete terms and built an auto-complete around them.
First, you'll want to make some type of auto-complete controller. It should take a string and return all possible auto complete terms for that string.
-(NSArray *)completionsForString:(NSString *)myString;
Then, check out the UIMenuController class. It's the class that shows the cut/copy/paste options in many applications. You can get the shared instance of it, populate the menu items yourself, and show it above the text field. The user can then simply tap the term they want.
In the end, the solution worked really well for our needs.
Alternatively, you can use this UITextField subclass (inspired by DOAutocompleteTextField):
https://github.com/hoteltonight/HTAutocompleteTextField
It's got a few more features and is actively developed. The example shows you how to use an array as the data source for the autosuggest text. It takes the same approach as DOAutocompleteTextField, in that it shows the suggested completion text "ghosted" in the text field as the user types.
Have you looked into UISearchDisplayController? There are a few threads here on Stack Overflow, including Core Data references if that is what you are using. Also some alternative methods, elsewhere.
With the help of the aforementioned Ray Wenderlich tutorial, I just implemented a version of this to filter names in an existing UITableView.
I set my text field's delegate as my view controller, my view controller as a UITextFieldDelegate and implemented these two methods:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
NSMutableArray *autoCompleteArray = [[NSMutableArray alloc]init];
[self retrieveData];
for(NSString *curString in _staffTableArray)
{
NSString *lowerCaseCur = [curString lowercaseString];
NSRange substringRange = [lowerCaseCur rangeOfString:substring];
if (substringRange.location == 0)
{
[autoCompleteArray addObject:curString];
}
}
if (![substring isEqualToString:#""])
{
_staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
}
[_staffListTableView reloadData];
}
use this delegate method. you can replace values that you specify.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
if ([string isEqualToString:#"StackO"]) {
textField.text=#"StackOverflow";
}
return YES;
}
Just faced with this thread because I need something similar. How about implementing you own search with the UITextfieldDelegate's method:
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
As you'd probably know this method is called for every UITextfield's typing.