UITextField add cleared icon [duplicate] - ios

Some apps have this nice little white circle with an gray x in there UITextField. How can I add this to an UITextField and delete the text when it is tapped?

To add a button like this, you can use the clearButtonMode property of an UITextField.
The code will be like this:
// Show cancel button never
textField.clearButtonMode = UITextFieldViewModeNever;
// Show cancel button only when you're editing text in textField
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// Show the cancel button only when you aren't editing the text
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
// Show always the cancel button
textField.clearButtonMode = UITextFieldViewModeAlways;

Check out the clearButtonMode property.

Related

Issues with clearButtonMode, showing button for empty text

How do you get the clear button to appear on a UITextField when the placeholder is visible? The following works but is only visible when selected and text has been entered.
Clear Button (when text entered)
textField = UITextField(frame: someFrame);
textField.placeholder = "Untitled";
textField.clearButtonMode = .always;
textField.clearsOnBeginEditing = true;
How do you get the clear button to appear while editing and empty?

Show Clear Button of UITextField even it is empty

I have a UITextField for search in my Application.
When user taps on the Clear Button in the right of the Search Text Field I need to hide this textfield.
So, can I show the Clear Button then TextField is empty? I need to do it always to provide the ability hiding TextField.
Just create a Custom button and set it as rightView for your TextField
UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// Set clear image for your button
[cleanBtn setImage:img forState:UIControlStateNormal];
[cleanBtn addTarget:self action:#selector(cleanTextField:) forControlEvents:UIControlEventTouchUpInside];
yourTextField.rightViewMode = UITextFieldViewModeAlways;
yourTextField.rightView = cleanBtn;
I tried to fix this issue with creating a custom button and setting the rightView of my text field. It did not solve my problem. There is no button on the right view.
I have added UIButton in Interface Builder using Auto Layout and placed it on my text field. It works. It's strange solution but it works.

How to align text in UITextField?

Is it possible to align placeholder in the middle of a UITextField? And when we start editing it should it start from left corner of UITextField? I am new to iOS.
Here is the sample code for that.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
txtList.textAlignment = NSTextAlignmentCenter;
}
// This method is called once we click inside the textField
-(void)textFieldDidBeginEditing:(UITextField *)textField{
txtList.textAlignment = NSTextAlignmentLeft;
}
Don't forgot to give delegate of UITextField.
Start by setting textField.textAlignment = NSTextAlignmentCenter. Your best bet is to implement the use of the UITextFieldDelegate protocol to know when the user is tapping inside your text field. Once the user has tapped inside your text field change the property to textField.textAlignment = NSTextAlignmentLeft if you want the alignment to go back to the left. Additionally, using the same protocol, when the user taps out or the focus leaves your text field, set the property back to textField.textAlignment = NSTextAlignmentCenter.
Swift 3.0 ... Swift 5.5
textField.textAlignment = .center

UISearchBar iOS 8 clarification

I'm trying to get special behavior of UISearchBar. I want never to show "Cancel" button (this part i'm already implement) and show Bookmarks button always.
But if i hide cancel button, bookmarks button is hide too in editing mode (only if searchBar.text.length > 0)
How i hide cancel button:
UITextField *textField = [searchBar valueForKey:#"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;
Picks:
My question is how can i always show only bookmarks button?
You also need to set this:
textField.rightViewMode = UITextFieldViewModeAlways;

UITextField and UIButton

There is a textField and a button in my view, and if the
textfield is empty I do not want to user can click the button.
When user text something in the textfield, the button will bu
clickable.
How can I do this? Thank you.
for your UITextField, you can set:
[textField addTarget:self action:#selector(editing:) forControlEvents:UIControlEventAllEditingEvents];
Then in your editing: have:
-(void)editing:(UITextField *)sender {
myButton.enabled = ![sender.text isEqualToString:#""];
}
Listen for changes to the text field. As the text changes, update the button's enabled property based on whether there is text or not. Of course you also need to set the button's state at the beginning as well.
// Setup the text field change listener (this can be done in IB if appropriate)
// Put this in viewDidLoad if not using IB.
UITextField *myTextField = ... // a reference to the text field
[myTextField addTarget:self action:#selector(textFieldChangedAction:) forControlEvents:UIControlEventEditingChanged];
// Initialize the button's state (put this in viewDidLoad)
myButton.enabled = myTextField.text.length > 0;
// The method called as the text changes in the text field
- (void)textFieldChangedAction:(UITextField *)sender {
myButton.enabled = sender.text.length > 0;
}
If you want, you can "hide" the button by setting the alpha value of the button to 0 and when the textfield is at least one character long, then set the button's alpha value to 1 to "show" the button. I think this conceptually is easy to do and very presentable to the user.
Set the textfield.delegate to self
Then in delegate that is textfielddidbeginediting
(
Check is textfield is #""
Then disable the button
Else
Enable the button
)
And there is one more. Delegate u can use
Textdidchangecharacters
And in viewdidload first state of the button should be disabled

Resources