I place some textfields in my app,those textfield allows only numbers 0,1..9 and .symbol.
For that my code is
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
static NSCharacterSet *charSet = nil;
if(!charSet) {
charSet = [[[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet] retain];
}
NSRange location = [string rangeOfCharacterFromSet:charSet];
return (location.location == NSNotFound);
}
It works fine,
But when ever i press ABC & #+= buttons keypad changes to another type.
I need to prevent that,user can use numbers and .,X keys only.
Rest of them not active ,if they are visible that not the problem
How can i done this.
can any one ple help me.
Thank u in advance.
There is a keyboard designed for this: UIKeyboardTypeDecimalPad
If you can target your app for iOS 4.1 and later, use #jtbandes suggestion.
The main advantage of using built-in keyboard types in iPhone SDK is they support internationalization. If you don't need letters, just numbers, I would suggest just building a custom keyboard from UIButtons with just the keys you want. Keep the keyboard from displaying (resignFirstResponder on textFieldShouldBeginEditing). You can include playing "Tock.aiff" from the "com.apple.UIKit" bundle to make it seem authentic.
If you need to support OS versions prior to 4.1, you can do what App Cubby did in some of their apps (like Gas Cubby) - they used the number pad keyboard (UIKeyboardTypeNumberPad) and overlaid a UIButton containing the dot in the bottom left corner. The keyboard handles the 0-9 input and the action for dot button is configured to append a '.' to the current string value.
Related
I want to prevent cursor back movement in UITextField. or I need to prevent entering text between two characters which is already entered.
How can I do this?
There is no property or method to prevent the user from using the magnifying glass on a particular text field. You can try to block all the characters inside the string:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(range.location < textField.text.length) return NO; else return YES;
}
After some research, I found this question: Disable Magnifying Glass in UITextField. The author of the accepted answer says there's a way to do it:
You can also set userInteractionEnabled to NO so that the user can't
tap the field. Call becomeFirstResponder manually so that the field
gets focus since the user can't tap to focus.
But, as I found out, it doesn't work, since setUserInteractionEnabled:NO prevents the text field from becoming the first responder. After the second research I found another question: Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO. MaxGabriel user claims he found the way to do it:
What I do is add that hidden text field to the view, and call
becomeFirstResponder on it. The user has no idea this text field
exists. In the delegate callback from the text field, I take the text
the user typed in and add it to a UITextView (though you could add the
text to whatever you wanted, like a UITextField like in your
question). I turn off userInteractionEnabled for the visible text
view. This creates the effect you desire.
Stack Overflow is a huge database, a little bit of research before asking a question may help you in the future.
For stopping entering text in UiTextField, you should use uitextfield delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Set tag for a textfield
if(textField.tag==10)
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength < [textField.text length]) ? NO : YES;
}
}
Modify the logic as per your requirement.
For preventing entering text between two characters Use NSRange and textfield text of range is less than textfield text length then return No then text will not be entered in UITextField
You cannot prohibit caret moving, but you can use UITextFieldDelegate's method textField:shouldChangeCharactersInRange:replacementString: for testing if text was typed inside already typed string, if it's not then change text in textfield manually and move caret to the end with the help of selectedTextRange property of UITextInput protocol.
I know that if you use the following line of code, you can detect what the user has typed in a TEXT VIEW.
if ([[textView text] isEqualToString:#"")
I want to detect whether the user has typed anything in a text FIELD, and this does not work with text fields. What should I change? Thanks for your help!
The UITextFieldDelegate is the preferred way to find this out.
- (void)textFieldDidBeginEditing:(UITextField *)textField - this will only tell you that it has become the first responder / key field, this does not guarantee that the user modified the value
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - this method will tell you when ever the user hits a key to change the text, a "paste" will cause multiple characters to be in the string, backspace over one, or delete of a selection will cause the string to be empty, …
- (void)textFieldDidEndEditing:(UITextField *)textField - this is a common location to check the value in the field against the value in the model to see if an edit has occurred. this will only be called when the field relinquishes "key" status, note that a user could tap a button to cause the whole view to go away before this is called, often making it useful to track if any field you are interested in becomes the "key field" (see first method)
it can be useful to set break points in relevant implementations to familiarize yourself with their call order/logic
You can retrieve contents of a UITextField the same way you can a UITextView, by accessing the "text" property. The code below is if a text field is not empty.
if (![someTextField.text isEqualToString:#""])
{
NSLog(#"someTextField is not empty");
}
You can also use the UITextField delegate to detect when a user starts typing, stops typing, etc. More information about that is available in the documentation.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/doc/uid/TP40006991
Finally, here's a link to the UITextField docs.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/doc/uid/TP40006888
you can check for text length instead of content
if ([[textView text] length]==0)
I have a UITextField only allow users to enter the number, which is amount of money. when user types 10000, the textfield will show € 10.000. And it will change dynamically when user types or backspace in the textfield. How do I implement it in iPhone?
You'd want to give the UITextField a delegate and implement something for
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
in that delegate that checked to see if the field's format was valid and altered the text if so, inserting the characters in the selected location, inserting / removing the . / currency symbol, then returning NO.
The tricky part is that the insertion point jumps to the end of the field whenever you replace the field's text - there are some undocumented APIs to retrieve / set its position, but not any official ones. Not a problem in most cases, since a user isn't very likely to move the insertion point in a field this small, but you might get a few complaints.
and thanks for your responses in advance.
I have been looking around for the possibility of having a 10-key, numeric only, input available when a user clicks on certain fields that do not require the use of the full size keyboard.
I know that popovers can properly display custom types of input, but does anyone know if there is a way for a standard 10-keypad to display in a popover? And if so, can you point me in the right direction.
Again, thanks in advance.
-Rick
You don't need to use popover if you try to enter numeric value in a UITextField/UITextView.
You can replace the keyboard by your own view.
You just do :
[yourTextField setInputView:myInputView];
You can have a look at KeyboardAccessory sample from apple
http://developer.apple.com/iphone/library/samplecode/KeyboardAccessory/Introduction/Intro.html
In this example, they provide an accessory view to the input view but if you modify the code and set inputView instead of inputViewAccessory, you should be able to do what you need.
If you use a UITextView, you can append text where the carret is with the following code:
NSMutableString *text = [textView.text mutableCopy];
NSRange selectedRange = textView.selectedRange;
[text replaceCharactersInRange:selectedRange withString:#"\n"];
textView.text = text;
If you use a UITextField, it seems you can only append text at the end of the string (no mean to retrieve carret position).
I have a UITextField for which I get input from some buttons that are part of the UI (so the input does not come form the phone's virtual keyboard). I want to limit that number of characters. I tried using the shouldChangeCharactersInRange, but it only works if the input comes form the virtual keyboard, and it doesn't work if the input comes from the buttons on the UI. Is there anyway I can solve this without programmatically having to count the characters in the text field every time I want to update it?
Thank you,
Mihai Fonoage
Implement the UITextField Delegate method shouldChangeCharactersInRange:
set the method to return YES when the length of the string is less than your maximum count.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length<3) {
return YES;
}
return NO;
}
Since you are programmatically making changes to the UITextField once the user clicks the relevant buttons on the UI, you should keep track of the number of characters entered using a counter.
Increase the value of the counter every time a relevant UI button is touched. Update the textfield only if the counter value is <= maximum allowed no. of characters.
Check the length of the string property on the textField before accepting changes.
There is a textRectForBounds Property