Adding text to UITextField you can't delete - ios

What I would like to do is to add some text at the beginning of UITextField that cannot be deleted. I add this text when the user taps on UITextField like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.text.length == 0) {
textField.text = #"myText";
}
return YES;
}
How do I prevent the user from deleting this text in a normal way? (without adding a separate UILabel on top of the TextField, for example).
Thanks in advance!

You can implement this code in shouldChangeCharactersInRange delegate method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length == 1 && [string isEqualToString:#""]) {//When detect backspace when have one character.
textField.text = #"myText";
}
return YES;
}

Checkout shouldChangeCharactersInRange in the UITextFieldProtocolReference
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
the text field calls this method whenever the user types a new
character in the text field or deletes an existing character.
So, you have control on whether text actually gets changed so you can insert your custom logic in that delegate callback enforcing whatever rules about what can change.

Use this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([[textField text] length]==LENGTH_OF_TEXT && [string isEqualToString:#""])
{
return NO;
}
else
return YES;
}
Hope it helps you.

Related

How to detect end of text when writing with keyboard?

I want to detect that text has end on text field when user press delete button to delete the text in the textfield.So when user reach to end of deleting the text then i want to detect it.
I have use following to achieve the task but i am unable to do it.Problem is event after textfield become empty then it does not detect.When i again type after textfield become empty then it detects but that time size is 1 not zero.Please suggest me some idea?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.txt_Search)
{
NSLog(#"out if");
if (![string isEqualToString:textField.text] )
{
if([self.txt_Search.text length]==0)
{
NSLog(#"finished writing");
}
}
}
return YES;
}
You can check the range to do the trick:
-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSRange backspaceEndRange = NSMakeRange(0, 1);
if (NSEqualRanges(range, backspaceEndRange)){
NSLog(#"finished writing");
}
return YES;
}

Restrict deletion on UITextField

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

iOS UItextfield reload after key hit

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

Set max length of UITextField in a UIAlertView

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

UitextField shifts control to next textField upon using keyboard input

I am new to iPhone programming. I have two textfields in iPhone with numberPad Keyboard type and i am trying to implement a simple logic that on typing a single digit using numberPadKeyBoard, the control should shift to next textField i.e. second textfield should become FirstResponder. I don't know how to implement this. Please guys any help would be appreciated.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == urFirstTextField) {
[urFirstTextField resignFirstResponder];
[urSecondtextField becomeFirstResponder];
}
}
UPDATE
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField != urFirstTextField)
{
[textField resignFirstResponder];
[urFirstTextField becomeFirstResponder];
return NO;
}
return YES;
}
Suppose you have 2 text field textField1 and textField2 then implement the delgate methods of the UITextFieldDelegate as
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if(textField == textField1)
{
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
return YES;
}
go through the delegate method of UITextField named "textField:shouldChangeText:inRange:" ..... check the length of your textField and make second textField as first responder before returning YES;
Or you can play with a lot of other delegate methods defined

Resources