I want to highlight a uitextfield when a user is editing it, so I set my textfield's borderstyle default to UITextBorderStyleNone and use the uitextfields delegates as following:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setBorderStyle:UITextBorderStyleBezel];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField setBorderStyle:UITextBorderStyleNone];
}
The Bezel style gets set and rendered, but when the endediting is called, the none style is not applied.
I tried changing the none to another (say rounded rect), but that one does render properly.
Does anybody know how I can get this to work?
Yes, it is a bug. Anyway, I've solved by using this code:
textField.borderStyle = UITextBorderStyleLine;
textField.borderStyle = UITextBorderStyleNone;
This isn't a very beautiful method, but it does the work until Apple fixes this issue.
I had this issue also. From my testing it looked as if I could set it to any style other than UITextBorderStyleNone.
A workaround that worked for me at least was disabling and re-enabling the textfield as seen below:
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.enabled = NO;
textField.borderStyle = UITextBorderStyleNone;
textField.enabled = YES;
}
I don't really like it but it works for now.
Related
My problem is that I want to make a UITextField like WhatsApp verification code textField(like below image).Where default value contains number of character to be entered(as hint for user). When user enter text replace hint character with entered character one by one?
Any help is much appreciated.
Please refer below link of my previous answer. Hope you will get an idea how to do this. Only single UITextField, No third party library use.
link- https://stackoverflow.com/a/36769911/5097148
for don't use of paste make couple of changes-
add myLable above textField then add this code in viewDidLoad()
self.myLable.userInteractionEnabled=true;
UITapGestureRecognizer *LongPressgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPressgesture:)];
tapPressgesture.delegate=self;
[self.myLable addGestureRecognizer:tapPressgesture];
and
- (void)tapPressgesture:(UITapPressGestureRecognizer *)recognizer
{
[self.txtField becomeFirstResponder];
}
for background like your image you can set textField background image property or add bottom border to your textfield. UITextField border style must be UITextBorderStyleNone If you have any issue then let me know.
Happy coding:)
After adding 6 Textfields and background image.
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField==textfield1)
{
[textfield1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
else if (textField==textfield2)
{
[textfield2 resignFirstResponder];
[textField3 becomeFirstResponder];
}
}
continue till textfield 6
I want to know how to disable UITextField, i.e I placed a UIButton in the frame of UITextField for design purpose.
When I tap my button in UITextField the keyboard appears, but I don't want the keyboard to be displayed!
Here is my code so far:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return textField !=textfiled1;
return textField !=textfiled2;
}
You can enable or disable user interaction on the textfield using this property:
textField.userInteractionEnabled = NO;
In Swift 5
textField.isUserInteractionEnabled = false
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
You can use this too
Accepted answer in Swift 4:
textField.isUserInteractionEnabled = false
I think your UIButton is not on UITextField. so bringToFromView first then your UIButton selector method called first. Because compiler check Event in hierarchy. if first control response the event then it wan't looking for next.
I have tested your scenario and its working fine at my end.
swift version of my Obj-C Answer
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
Use the below property.
textField.userInteractionEnabled = NO;
you can achieve this with the help of:
If you want to disable editing you can do this
textField.editable = NO;
and if you want to make it editable again you can do this
textField.editable = YES;
I'm wondering how to disable the inputview of a UITextfield. Setting textField.inputView = nil; or [textField setInputView:nil] in ShouldBeginEditing doesn't do anything, and using the userInteraction property removes the ability to interact with the field. Ideally, I'd like to remove both the cursor and the keyboard while still being able interact with and switch between textfield methods, using ShouldBeginEditing and ShouldEndEditing. Is there any way to accomplish this?
You should do this:
myTextField.inputView = UIView.new; //Empty UIView
Setting it to nil just means the default keyboard is used.
To get rid of the caret, subclass the UITextField and override caretRectForPosition:
- (CGRect) caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
Try this :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide both keyboard and blinking cursor.
}
or
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder]; // hides keyboard
}
This question already has answers here:
Adding the "Clear" Button to an iPhone UITextField
(11 answers)
Closed 2 years ago.
In my application, I am adding a UISearchBar.
My intent is to enable the UISearch Bar "X button"(clear button in UITextField) to be always visible.
I have tried using the following code below to try to make the "X Button" be always visible. However, it does not work. If I set tf.clearButtonMode = UITextFieldViewModeNever, the clear button in uitextfield not showing. I am not sure what is wrong?
I would really appreciate anyone's help here. Why is this not working?
Code (Not working)
for (UIView* v in searchBar.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
{
UITextField *tf = (UITextField *)v;
tf.delegate = self;
tf.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
Goal:
I want to always show the clear button if the text length is equal to 0
i.e. if I don't input any text.
UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
searchBarTextField.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
This is the default behavior of the search bar. Because if the UITextField is blank then there is no need to press it.
U can do it in Xib. I am attaching the screenshot.
And programmatically
myUITextField.clearButtonMode = UITextFieldViewModeAlways;
I tried to get it but unfortunately , There is no Way of Customising with the ClearButton(X) of UITextField .
There is a way that If You only need it to get resign the KeyBoard , Then just overriding this method :
Just clear the field yourself and call resignFirstResponder .
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
Documentation about it HERE
This is an older question but I came here with an equal customer request: "Show the clearButton as soon as the cursor is in the searchField. We want to be able to cancel the search with this button in any stage".
I came up with a solution other than adding a custom button:
AppleDocs:
UITextFieldViewModeAlways The overlay view is always displayed if the
text field contains text.
So adding a whitespace as the first character will set the clearButton active.
The leading whitespace can be removed as soon as text is entered in the searchField or at any other point before using the text.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//adding a whitespace at first start sets the clearButton active
textField.text = #" ";
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
...
NSString *completeNewString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//remove the dummyWhitespace (here or later in code, as needed)
self.searchString = [completeNewString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
...
return YES;
}
I want to remove the border of UITextField dynamically.
I tried [stringTextField setBorderStyle:UITextBorderStyleNone];
but nothing happened. Any idea?
Is the TextField already displayed in the view when this happens? If so, you (probably) need to execute the following:
[stringTextField setBorderStyle:UITextBorderStyleNone];
[stringTextField setNeedsDisplay];
in order for the view to redraw the TextField, sans border. Note that there's no guarantee the system will immediately redraw the textField. You're indicating to the system that you'd like the field to be redrawn.
With an existing UITextField I found that this worked:
[textField setEnabled:NO];
[textField setBorderStyle:UITextBorderStyleNone];
while this did not (the border remained in the view):
[textField setBorderStyle:UITextBorderStyleNone];
[textField setEnabled:NO];
Try this ones.
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.borderStyle = UITextBorderStyleNone;