I set
textField.clearsOnInsertion=YES
but nothing happened. I am using XCode 5.0 and iOS 7.0
You should firstly make it clear what is clearsOnInsertion. See Apple's doc:
clearsOnInsertion
A Boolean value indicating whether inserting text
replaces the previous contents.
Discussion
The default value of this property is NO. When the value of
this property is YES and the text view is in editing mode, the
selection UI is hidden and inserting new text clears the contents of
the text view and sets the value of this property back to NO.
clearsOnInsertion is very useless in my opinion. See the screenshot:
The text field is in editing mode and the keyboard is shown but the selection UI is hidden. When you input any thing, it will replace the old text. That's it.
Another related useful property is clearsOnBeginEditing
clearsOnBeginEditing
A Boolean value indicating whether the text field removes old text when editing begins.
Try this:
textField.clearsOnBeginEditing = YES;
if you want to clear previous text when editing begins you should use
textfield.clearsOnBeginEditing = YES
Related
Im my application when the user selects a TextField the existing text gets automatically selected when the Focus changes to the textfield. How do I hide the blue selection handles that appear?
The reason I want to automatically select the existing text is so that the user can begin typing and the textfield will automatically overwrite the existing value instead of appending to it.
I figured it out. You can set the enableInteractiveSelection property to false.
I faced with an interesting problem. In my app, I have some UITextFields that could contain only one character inside.
From iPhone 6s (iOS 9) Apple has introduced a cool feature related with force touch on keyboard text field:
http://cdn.osxdaily.com/wp-content/uploads/2016/05/iphone-keyboard-trackpad.mov.gif
In my case when the text field can contain only one character is completely useless and can be very misunderstanding.
I went through the UITextField documentation and I haven't found anything that could help me.
Do you have any ideas how to solve this problem? Is it possible?
Thanks!
UPDATE
Related with #dmorrow answer:
Setting the isSecureTextEntry to true disables that feature, but in my case I want to see the input text. So the isSecureTextEntry must be false
I just tested, and textField.isSecureTextEntry = true disables this feature.
I'm not sure how you are switching your input from number to •, but if you just use the native password input you'll get the result you need.
New Idea
What if you used a hidden textfield for the input, with isSecureTextEntry = true. Each of your input fields would actually just be a UIButton. On tapping the button, the hidden textfield would become first responder. On text input, you could set the label of the button and jump to the next button as you are doing now. Any time the user taps the button, it would clear the input and allow them to type again.
I have an input field the user needs to fill with an alphanumeric code. The keyboard the user uses to type the code has a dynamic return button that changes to "send" as he writes some text on the field. When the field is empty the return button has the default value.
To dynamically change the return button type I use the following code:
if([textField.text isEqualToString:#""])
{
textField.returnKeyType = UIReturnKeyDefault;
}
else
{
textField.returnKeyType = UIReturnKeySend;
}
[textField reloadInputViews];
However this has the following drawback: since the code is alphanumeric, the user may be typing numbers, and yet the keyboard will always switch back to the letter keyboard, so to type more than one number in a row he will need to be continuously switching to number keyboard.
Is there any way to dynamically change the return key of a keyboard as the user types but to preserve the keyboard state to letters or numbers keyboard?
I think this is not a bug on Apple's side, more a missing implementation of API for the keyboard. With the new iOS8 API you might want to create your own keyboard returning the UIKeyboardType.
For iOS7 I worked around by inspecting the views of the keyboard. Use the US2KeyboardType CocoaPod or the source:
https://github.com/ustwo/US2KeyboardType
As Martin noted above, it's not a bug on Apple's side, but on my side. However I'll be posting the solution I've found since it is the one that solves that particular problem:
Instead of manually changing the return key type when there is text on the text field, Apple provides us with a property called enablesReturnKeyAutomatically that when set to YES it automatically manages the issue, enabling and disabling the return key depending on whether there is text or not in the text field.
Therefore you don't need to modify the returnKeyType property, and thus, no calling to reloadInputViews is required, so the keyboard doesn't change back to its original state.
How do I determine the current state of the UIResponderStandardEditActions for text formatting? For example, if I do the following:
[textView toggleBoldface:nil];
How do I query the textView to find out if the state of bold is now on or off? This is for just a cursor with no selection (i.e. range length is 0). As such, enumerateAttribute doesn't seem to work.
Thank you.
It appears the typingAttributes property (available in iOS 6) will log the attributes that will be applied to new text typed by the user, even with a selection length of 0. Thus revealing what the state of formatting options such as bold will be.
NSLog(#"textViewFormatting options: %#", [[self noteTextView] typingAttributes]);
I'm not finding anything useful in the docs, but I suppose it would be simple enough to just subclass UITextView, add a property on it like BOOL boldText and then wherever you call [textView toggleBoldFace:nil]; just toggle that property as well. And then when you need to check the state of the textView, just check the boldText property instead.
I want to add a variable place holder in my textField and when something is written in textField, that text should be appended to place holder without any change to place holder. How should I go about it?
you can do this by using clearsOnBeginEditing property and set it to FALSE.
textField.clearsOnBeginEditing = NO;
If you are creating textfield by interface builder then you have to go to inspector in Interface Builder(Press command 1) and finally you have to uncheck the Clear when editing begins option.