Delete text from backspace - ios

I can't find tutorial that does this. I have...
UIbutton
UItext.text
I want to make my own KEYBOARD user interface without iphone keyboard. I want to press the UIbutton(keyboard "delete" key or backspace) from text. Not ease the whole thing like UIText.text = #"". I've seen programmers give the wrong code. I'm looking for something like Visual Basic Sendkeys {Delete}. How do I do this on iphone button? http://gigaom.com/apple/iphone-os-where-the-delete-key-belongs/
For example:
Text1
Text<=
Tex<=

Implement the following method to delete the last character.
- (NSString *)deleteLastChar:(NSString *)str {
if (![str length]) return #"";
return [str substringToIndex:[str length]-1];
}
You can then use that on your text field's text property e.g:
textField.text = [self deleteLastChar:textField.text];

if ([textField.text length] != 0) {
textField.text = [textField.text substringToIndex:[textField.text length]-1];
}

Related

How to set a range of numbers for textfield in iOS?

I want to set a range i.e between $350000 & $800000 for a text field. If the entered number is less than this range then an alert message should pop up. Plz help
In Objective-C using NSNumberFormatter you can use like this:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.minimum = #10;
nf.maximum = #20;
NSNumber *a = [nf numberFromString:#"12"];
NSNumber *b = [nf numberFromString:#"22"];
//if the number is beyond the range, it will return nil, so in above b is nil
//so the following check will show the log and alert
if (!a || !b) {
NSLog(#"Either of them is out of range");
//show the UIAlert here
}
EDIT:
You should call the above in following scenarios:
User enter a value and does some action. In the beginning of the action use the above.
Or every time the user enters and moves away from the textfield, then call the above in textFieldDidEndEditing: delegate method of UITextField.
Note: Since your number has $ prefixed, please make sure you set the currency style in nf as well, instead of trimming it.
Use - (void) textViewDidEndEditing:(UITextView *)textView method to check the intergerValue of text and if it doesn't match your requirement show the popup.
If wanna check it on the go use - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
NSString *finalText = [textView.text stringByAppendingPathComponent:text];
finalText = [finalText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//enable button
if ([finalText integerValue] < 350000 || [finalText integerValue] > 850000) //do what you wanna do
Make sure $ is out of textfield or you remove it before using integerValue.
This can be helpfull
-(void)viewDidLoad
{
[txt addTarget:self action:#selector(CheckingMethod:) forControlEvents:UIControlEventValueChanged];
}
-(IBAction)CheckingMethod:(id)sender
{
if(txt.text.length>9) {
//your popupview
}
else{
}
}

delete last character of string with self-made keyboard

i have a uitextfield (_nameField) and buttons a-z, space, minus, delete and ok.
the buttons a-z, space, minus have the following same ibaction:
NSString *text1 = #"";
- (IBAction)keyboardButtons:(id)sender {
if (_nameField.text.length <=14) {
if (_nameField.isEditing) {
text1 = [text1 stringByAppendingString:[sender currentTitle]];
[_nameField setText:text1];
}
}
}
works perfectly.
the delete button have this ibaction:
- (IBAction)clearButton:(id)sender {
if (_nameField.isEditing) {
text1 = #"";
[_nameField setText:text1];
}
}
ok, thats pretty simple, it sets the text of my string to #"". but i want to delete just the last character. how can i do that?
just write
text1 = [text1 substringToIndex:(text1.length -1)]
Obj-C doesn't provide a method to remove the last character, but you can achieve the same thing by taking a substring that includes all characters except the last one.
NSString* withoutLastChar = [originalString substringToIndex: [originalString length] - 1];

VENTokenField hold backspace key to delete tokens

While trying to allow multi token deletions, as user holds the backspace key in VENTokenField to act the same as the native email app, or messages app, I have come across many problems...
First, I can only detect one tap on the backspace key from the initial code the VENToken's UITextField subclass offer (which is technically touching private API) - (BOOL)keyboardInputShouldDelete:(UITextField *)textField. That is fine, but not helpful for detecting long press on backspace button, which only works while you actually have characters in a certain UITextField, and not while the UITextField is empty such as in our case.
I have also came across this blogpost which suggest another approach of accessing more of the private API, however, does not offer solution to my problem. As it's not documented, I was wondering if there is a valid way to detect this event at all?
I've resolved it by first, comment out anything that was in VENBackspaceTextField class'
keyboardInputShouldDelete:(UITextField *)textField
Then, added 2 consts in VENTokenField header:
NSString * const kTextEmpty = #"\u200B"; // Zero-Width Space
NSString * const kTextHidden = #"\u200D"; // Zero-Width Joiner
Everytime the token becomes first responder, make sure the textField has the empty text:
- (void)inputTextFieldBecomeFirstResponder {
[self.inputTextField becomeFirstResponder];
if (self.tokens.count) {
[self.inputTextField setText:kTextEmpty];
}
...
}
And set it to hidden when cursor is not visible:
- (void)setCursorVisibility {
NSArray *highlightedTokens = [self.tokens filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(VENToken *evaluatedObject, NSDictionary *bindings) {
return evaluatedObject.highlighted;
}]];
BOOL visible = [highlightedTokens count] == 0;
if (visible) {
[self inputTextFieldBecomeFirstResponder];
} else {
[self.invisibleTextField becomeFirstResponder];
[self.invisibleTextField setText:kTextHidden];
}
}
Then, modified the textField Delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (self.tokens.count && [string isEqualToString:#""] && [textField.text isEqualToString:kTextEmpty]){
VENToken *lastToken = [self.tokens lastObject];
lastToken.highlighted = YES;
[_inputTextField setText:kTextHidden];
_inputTextField.alpha = 0.0;
return NO;
}
if ([textField.text isEqualToString:kTextHidden]){
[self deleteHighlighted];
[self unhighlightAllTokens];
return (![string isEqualToString:#""]);
}
//If there are any highlighted tokens, delete
[self deleteHighlighted];
return YES;
}

How can I recognize keyboard presses in ios?

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;
}

UITextFieldDelegate, checking if textFields have text in it

i have a little problem.
There are two textFields in my TableView which set the property of an object. In order to do so i want to force the user to write something in the textField before the string is actually been set to the object. So basically a simple ([textField.text length] > 0) thing.
But i want that the user have to write strings in both the two textFields to finally enable the "Done"-Button.
I solved this earlier but with only one text Field with the following UITextFieldDelegate method.
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
My solution for the new problem, so now with two textFields is this one:
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
if ([theTextField.placeholder isEqualToString:#"textField1"]) {
if ([theTextField.text length] > 0) {
enabledVokabel = YES;
} else {
enabledVokabel = NO;
}
}
if ([theTextField.placeholder isEqualToString:#"textField2"]) {
if ([theTextField.text length] > 0) {
enabledUebersetung = YES;
} else {
enabledUebersetung = NO;
}
}
self.doneBarButton.enabled = (enabledVokabel && enabledUebersetung);
return YES;
}
So i want the doneBarButton been enabled when both of the textFields (textField1 and textField2) are filled with text. But i want it that way that if the user has deleted the text he/she just wrote in the doneBarButton is disabled as soon as the textFields are empty.
It doesn't work that way. Do you have a solution? Or maybe a better way to solve it?
Either just connect value changed in interfacebuilder to a IBAction method in any of the classes you have in your view. Or you can do it in code with:
[textField addTarget:self
action:#selector(myIBActionMethod:)
forControlEvents:UIControlEventEditingChanged];
And check the length of the input.
You can of hook up both textfields to the same method and check the length of both textfields every time its called if you have IBOutlets to them both.
I'd keep a reference for both UITextViews, lets say.-
IBOutlet UITextView *textView1;
IBOutlet UITextView *textView2;
properly linked to your xib/storyboards. Also, I'd rather use
- (void)textViewDidChange:(UITextView *)textView
callback. According to shouldChangeCharactersInRange docs, it looks like this method is called before actually changing the text.
As for the enabled condition, it would look something like this.-
self.doneBarButton.enabled = [textView1.text length] > 0 && [textView2.text length] > 0;

Resources