When changing UITextField property isSecureTextEntry for the first time keyboard flashes - ios

When I change UITextField property isSecureTextEntry, the keyboards flashes.
But this happens only if once per app launch. Any ideas why and how to fix?
Code I am using is relatively simple, textField is inside tableViewCell, there is some setup method:
field.keyboardType = input.key.configuration.keybordType
field.textContentType = input.key.configuration.context
field.autocapitalizationType = input.key.configuration.capitalization
textField.isSecureTextEntry = input.key.configuration.isSecure
field.text = input.value
And then I toggle like this
field.isSecureTextEntry.toggle()

you can use like this
textField.isSecureTextEntry = true

Related

UItextField with Multiple lines

I am trying to make a form and the requirement is to have a UItextfield with multiple lines which means, when a specific number of characters are entered in one line the blip moves to next line also on hitting enter it should enter the next line as well.
Currently, I am working with Xcode 9.4 but it offers a single line text field.
I used UITextView with this code:
lazy var commentTextView: UITextView = {
// Create a TextView.
let textView: UITextView = UITextView()
// Round the corners.
textView.layer.masksToBounds = true
// Set the size of the roundness.
textView.layer.cornerRadius = 20.0
// Set the thickness of the border.
textView.layer.borderWidth = 1
// Set the border color to black.
textView.layer.borderColor = UIColor.systemGray.cgColor
// Set the font.
textView.font = UIFont.systemFont(ofSize: 16.0)
// Set font color.
textView.textColor = UIColor.black
// Set left justified.
textView.textAlignment = NSTextAlignment.left
// Automatically detect links, dates, etc. and convert them to links.
textView.dataDetectorTypes = UIDataDetectorTypes.all
// Set shadow darkness.
textView.layer.shadowOpacity = 0.5
// Make text uneditable.
textView.isEditable = true
return textView
}()
and this is the result
You should use UITextView for multiline string or there is third party library which you can use.
MultilineTextField

How can I hide quicktype keyboard toolbar on iPad?

How can I hide quicktype keyboard toolbar on iPad?
The following code doesn't work:
textField.autocorrectionType = UITextAutocorrectionTypeNo;
place this code in viewDidLoad
yourTextFieldName.autocorrectionType = UITextAutocorrectionTypeNo;
UITextInputAssistantItem* shortcut = [yourTextFieldName inputAssistantItem];
shortcut.leadingBarButtonGroups = #[];
shortcut.trailingBarButtonGroups = #[];
Swift
yourTextFieldName.autocorrectionType = .No
let shortcut : UITextInputAssistantItem = yourTextFieldName.inputAssistantItem
shortcut.leadingBarButtonGroups = []
shortcut.trailingBarButtonGroups = []
swift3
yourTextFieldName.autocorrectionType = .no
var shortcut: UITextInputAssistantItem? = yourTextFieldName.inputAssistantItem()
shortcut?.leadingBarButtonGroups = []
shortcut?.trailingBarButtonGroups = []
for reference
How to hide the shortcut bar in iOS9
Have you tried this yet? What you do is simply disable the text proposals, not the undo / redo / paste ... thingies.
To hide shortcuts altogether, set the leadingBarButtonGroups and trailingBarButtonGroups properties to nil. Doing so hides only the shortcuts and does not hide the typing suggestions. To hide typing suggestions, you must also set the autocorrectionType property of the responder that displays the keyboard to UITextAutocorrectionTypeNo.
<editorView>.autocorrectionType = UITextAutocorrectionTypeNo;
UITextInputAssistantItem* shortcut = [<editorView> inputAssistantItem];
shortcut.leadingBarButtonGroups = #[];
shortcut.trailingBarButtonGroups = #[];

UiTextField changes font while editing in Swift 1.2 & 2.0

I have a UITextField with a custom font, everything worked fine until Swift update to 1.2 and 2.0. Afterwards, each time I try to edit a text field, it changes its font to a different one that seems a sort of Times New Roman. Does anyone have experience of that?
I came across this same issue and figured out a solution. The problem boils down to setSecureTextEntry changing the font when it is set, and not changing it back when it is unset. In fact, you can never change the font back as long as your UITextField has first responder.
The trick is to resignFirstResponder before you call setSecureTextEntry: and then becomeFirstResponder again. This will work (as of iOS 9.2), but it triggers the keyboard show/hide animation and will cause the screen to "shake". To get around that, you'll need to kill the keyboard animation as well.
Here's my full solution:
- (void)setSecureTextEntry:(BOOL)secureTextEntry {
__weak typeof(self) weakSelf = self;
[UIView performWithoutAnimation:^{
BOOL resumeResponder = NO;
if([[weakSelf textEntryField] isFirstResponder]) {
resumeResponder = YES;
[[weakSelf textEntryField] resignFirstResponder];
}
[[weakSelf textEntryField] setSecureTextEntry:secureTextEntry];
if(resumeResponder) {
[[weakSelf textEntryField] becomeFirstResponder];
}
}];
}
PS: This isn't a Swift bug. It's a UIKit bug. I had the same issue with Objective-C.
I had a weird case of fonts changing its size and font type, when secureTextEntry for an UiTextField was toggled by using a button action.
Had to explicitly manage font for the UiTextField by using these lines of code:
password.font = UIFont(name: "systemFont", size: 14)
password.font = UIFont.systemFontOfSize(14)
Complete Code used in the Show Password Button:
//Function associated with the button for show password option
#IBAction func switchShowPasswordAction(sender: AnyObject) {
if showPassword{
showPassword = false
password.secureTextEntry = !showPassword
}else{
showPassword = true
password.secureTextEntry = !showPassword
}
//Changing font fix
password.font = UIFont(name: "systemFont", size: 14)
password.font = UIFont.systemFontOfSize(14)
}
Post applying this change:
Since I used custom fonts we need to preserve the original font. Create an extension to UITextField:
extension UITextField {
func enablePasswordModeWithShowHide() {
secureTextEntry = false
let showButton = UIButton(type: UIButtonType.System)
showButton.setTitle("HIDE", forState: .Normal)
showButton.titleLabel?.textAlignment = .Right
showButton.sizeToFit()
rightView = showButton
rightViewMode = .Always
showButton.addTarget(self, action: "handleShowHideTapped", forControlEvents: .TouchUpInside)
showButton.tintColor = UIColor.blackColor()
}
func handleShowHideTapped() {
secureTextEntry = !secureTextEntry
let font = self.font
self.font = nil
self.font = font
if let oldText = text {
text = "";
text = oldText;
}
if let button = rightView as? UIButton {
button.setTitle(secureTextEntry ? "SHOW" : "HIDE", forState: .Normal)
button.sizeToFit()
}
}
}
Where it could be implemented like this:
passwordTextField.enablePasswordModeWithShowHide()
All of these answers pretty much work, but I had to use a different solution to achieve the results I needed, considering I'm using a custom font.
You need to make the text field attributed in the storyboard inspector pane for the UITextField, as follows:
Then, in code, you need to manage the toggling of the visibility, setting the attributed text each time, to ensure its properly formatted. I also resignFirstResponder() on the field just to take care of some positioning glitch that I still haven't figured out yet.
func toggleShowPass() {
self.showing = !showing
txtpassword.secureTextEntry = !showing
textFieldPassword.resignFirstResponder()
let string = textFieldPassword.text!
let attrString = NSMutableAttributedString(string: string)
textFieldPassword.addAttribute(NSFontAttributeName, value: UIFont(name: "AvenirNext-Regular", size: 16.0)!, range: NSMakeRange(0, string.characters.count))
textFieldPassword.attributedText = attrString
}
Set defaultTextAttributes with custom font attribute after toggling the secureTextEntry flag
NSDictionary *attrsDictionary =
#{ NSFontAttributeName://customfont};
_passwordtextfield.defaultTextAttributes = attrsDictionary;
I had to apply the following solution with latest Xcode 7.1.1 which actually worked in my case I suspect this issue is of framework.
- (IBAction)btnPasswordShowAction:(id)sender {
self.txtPassword.secureTextEntry = !self.txtPassword.secureTextEntry;
NSString *tmpString = self.txtPassword.text;
self.txtPassword.text = #" ";
self.txtPassword.text = tmpString;
[self.txtPassword setFont:nil];
self.txtPassword.font = [UIFont fontWithName:#"OpenSans-Regular" size:16.0];
}
#pragma mark - Textfield Delegate Methods
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.txtPassword setFont:nil];
self.txtPassword.font = [UIFont fontWithName:#"OpenSans-Regular" size:16.0];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[self.txtPassword setFont:nil];
self.txtPassword.font = [UIFont fontWithName:#"OpenSans-Regular" size:16.0];
return YES;
}

UITextField has trailing whitespace after secureTextEntry toggle

I have a button that toggles between Show/Hide mode (i.e. toggles a UITextField between secureTextEntry NO and YES). The purpose of which is to allow the user to see the password they are entering.
I followed the example (with the highest number of votes) here: UITextField secureTextEntry - works going from YES to NO, but changing back to YES has no effect
However, when I set secureTextEntry to NO, any text that was written there ends up with a space at the end. This does not seem to be a problem when setting secureTextEntry to YES.
For example, if I enter the text "mypassword" while setSecureTextEntry is set to NO, and then switch it to YES, the user will see ********** (10 dots), which is correct. If I setSecureTextEntry to NO, the user will see "mypassword " (with a space at the end, or at least, the cursor moved one space to the right).
Important note: In the debugger, the string value of text appears without the trailing space, like this:
(lldb) expr self.passwordText.text
(NSString *) $0 = 0x1d8450e0 #"mypassword"
I have tried trimming whitespace (per avoid middle whitespace in UITextField), but it has had no effect.
i've just encounter this case and finally solved this problem.
works on Latest iOS SDK, iOS 8.1
First of all, there is no trailing space at all.
The dot(shown in SecureEntry) character and normal character have different width and after you toggle isSecureEntry switch, the cursor didn't refresh it's position.
so i use this workaround to solved this problem.
- (void)toggle
{
NSString *tmpString;
[self.passwordTextField setSecureTextEntry:!self.passwordTextField.isSecureTextEntry];
if (self.passwordTextField.isSecureTextEntry) {
// do stuffs
} else {
// do other stuffs
}
// Workaround to refresh cursor
tmpString = self.passwordTextField.text;
self.passwordTextField.text = #" ";
self.passwordTextField.text = tmpString;
}
Swift 3+
// Workaround to refresh cursor
let currentText: String = self.userPassword.text!
self.userPassword.text = "";
self.userPassword.text = currentText
hope it helps!
PRE-iOS-8.0 (dated solution)... In your button's action method (toggling between secureTextEntry YES and NO), simply set UITextField's text property to its current text value. Although this may seem redundant and a bit like a hack, this will redraw the cursor in the right position. Here's an example of what your button's action method should look like now...
-(void)toggleButtonPressed:(UIButton*)sender
{
// Toggle between secure and not-so-secure entry
self.toggleButton.selected = !self.toggleButton.selected;
self.textfield.secureTextEntry = !self.toggleButton.selected;
// * Redundant (but necessary) line *
[self.textfield setText:self.textfield.text];
}
POST-iOS-8.0... As of iOS 8.0, it appears that UITextField's text setter no longer redraws the cursor when called with a string equal to its current string value. Now, we need to take this a step further and actually change the text value before resetting it again. Replace the above setText: line with something like these lines.
// * Extra redundant (but necessary) lines *
NSString *currentText = self.textfield.text;
[self.textfield setText:#"Arbitrary string..."]; // Change the value
[self.textfield setText:currentText]; // Reset the value
I have a clean solution not going dirty with text property of UITextField.
Wrap them in this style.
[self.passwordTextField resignFirstResponder]; // first resign its first responder.
// change `secureTextEntry` property's value if necessary.
if (self.passwordTextField.secureTextEntry) {
self.passwordTextField.secureTextEntry = NO;
self.passwordEcryptButton.selected = YES;
}else{
self.passwordTextField.secureTextEntry = YES;
self.passwordEcryptButton.selected = NO;
}
[self.passwordTextField becomeFirstResponder]; // finally gain its first responder again.
In order to work around this bug in iOS you can simply do the following (works for any iOS version):
- (IBAction)toggleSecureTextEntry:(UIButton *)button
{
self.textField.secureTextEntry = !self.textField.secureTextEntry;
NSString *originalText = self.textField.text;
self.textField.text = nil;
self.textField.text = originalText;
}
You can fix it like this:
NSString *currentText = self.textfield.text;
self.textfield.text = #"";
self.textfield.text = currentText;
This work for me on iOS 8
if (self.passwordTextField.secureTextEntry) {
// Display password and keep selected text range
UITextRange *selectedTextRange = self.passwordTextField.selectedTextRange;
NSString *password = self.passwordTextField.text;
self.passwordTextField.secureTextEntry = NO;
self.passwordTextField.text = [#"" stringByPaddingToLength:password.length withString:#" " startingAtIndex:0]; // Done for carret redrawing
self.passwordTextField.text = password;
self.passwordTextField.selectedTextRange = selectedTextRange;
}
else {
// Hide password and keep selected text range
UITextRange *selectedTextRange = self.passwordTextField.selectedTextRange;
NSString *password = self.passwordTextField.text;
self.passwordTextField.secureTextEntry = YES;
self.passwordTextField.text = [#"" stringByPaddingToLength:password.length withString:#" " startingAtIndex:0]; // Done for carret redrawing
self.passwordTextField.text = password;
self.passwordTextField.selectedTextRange = selectedTextRange;
}
UITextPosition *beginning = [self.passwordField beginningOfDocument];
[self.passwordField setSelectedTextRange:[self.passwordField textRangeFromPosition:beginning
toPosition:beginning]];
UITextPosition *end = [self.passwordField endOfDocument];
[self.passwordField setSelectedTextRange:[self.passwordField textRangeFromPosition:end
toPosition:end]];
This is what I used for iOS 8
When we change a textfield.secureTextEntry property, the caret position is not updated. To fix this, the code below used to work before IOS 8:
pwdTextField.text = pwdTextField.text
Now it doesn't. It seems IOS 8 detects the new value equals old value and does nothing. So to make it work again we have to actually change the value. Here is the swift version that works for me.
let str = pwdTextField.text
pwdTextField.text = str + " "
pwdTextField.text = str
This is another possibility to solve this issue, where self.passwordText is the UITextField:
if (self.passwordText.isFirstResponder) {
[self.passwordText resignFirstResponder];
[self.passwordText becomeFirstResponder];
}
It appears that the second solution in the referenced link, when implemented, has the desired behavior of not adding an extra space:
https://stackoverflow.com/a/8495888/738190
This Works in my case
BOOL wasFirstResponder = [self.passwordTextField isFirstResponder];
if([self.passwordTextField isSecureTextEntry])
{
//This three lines are key
self.passwordTextField.delegate = nil;
[self.passwordTextField resignFirstResponder];
self.passwordTextField.delegate = self;
}
[self.passwordTextField setSecureTextEntry: !self.passwordTextField.isSecureTextEntry];
if(wasFirstResponder)
[self.passwordTextField becomeFirstResponder];
Swift UITextField extension:
extension UITextField {
func toggleSecureEntry() {
let wasFirstResponder = isFirstResponder
if wasFirstResponder { resignFirstResponder() }
isSecureTextEntry.toggle()
if wasFirstResponder { becomeFirstResponder() }
}
}
Setting textField.text solution also works in some situations but not for my need (Custom font with two text fields. Caused font changes and glitches on runtime.) Adding here too.
func toggleSecureEntry() {
isSecureTextEntry.toggle()
let originalText = text
text = nil
text = originalText
}
To get the cursor to reposition correctly, setting the font attributes seemed to do the trick for me.
// Hack to update cursor position
self.passwordTf.defaultTextAttributes = #{NSFontAttributeName: textFieldFont, NSForegroundColorAttributeName: textFieldColor};
// Change secure entry
self.passwordTf.secureTextEntry = !self.passwordTf.secureTextEntry;
Tested on iOS8, iOS9.
Hope it helps!
Everytime the text is set in the UITextField, the cursor postition is updated
So I used this code
partial void btnShowPassword_ToutchUpInside (UIButton sender)
{
if (TxtPassword.SecureTextEntry == true) {
TxtPassword.SecureTextEntry = false;
TxtPassword.Text = TxtPassword.Text;
} else {
TxtPassword.SecureTextEntry = true;
}
}
Here is the solution:
- (void)showHidePassword:(UIButton *)sender {
EDSignUpCell *cell = [self.signUpTblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
if(!TRIM_SPACE(cell.cellTextField.text).length) {return;}
[cell.showHidePasswordBtn setSelected:!cell.showHidePasswordBtn.isSelected];
cell.cellTextField.secureTextEntry = cell.showHidePasswordBtn.isSelected;
[cell.cellTextField setText:cell.cellTextField.text];
[cell.cellTextField becomeFirstResponder];
}
I'm using this, Works fine.
[self.yourTextField becomeFirstResponder];
Swift 4
Bug is on radar, there is explanation of workaround also: http://www.openradar.me/38465011
Here is cut of temporary workaround how to natively update caret (cursor) position.
// update caret position
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
let (beginning, end) = (self.beginningOfDocument, self.endOfDocument)
self.selectedTextRange = self.textRange(from: beginning, to: end)
self.selectedTextRange = self.textRange(from: end, to: end)
}
I had a similar issue and realized it was because I was updating the text before setting the secureTextEntry property. It makes sense that the textField would draw out the caret at the location it'd be at if it were using secureTextEntry.
I did not read the entire problem nor did I visit the solution linked by OP, but in case someone else has the same issue as me:
Try updating your text after setting the secureTextEntry property.

Disable UISearchBar

I have a -as I hope- very simple question: how to disable an UISearchBar in IOS5 to avoid user interaction? I can't find setEnabled or something like this...
Thanks!
Have you tried:
[searchBar setUserInteractionEnabled:NO];
?
In addition to setting user interaction, I also adjusted the alpha value as well, to make the appearance unique.
searchbar.alpha = .75;
Try this
// Normal
self.searchDisplayController.searchBar.userInteractionEnabled = YES;
self.searchDisplayController.searchBar.translucent = YES;
self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleDefault;
self.searchDisplayController.searchBar.backgroundColor = [UIColor clearColor];
// Faded out
self.searchDisplayController.searchBar.userInteractionEnabled = NO;
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchDisplayController.searchBar.backgroundColor = [UIColor lightGrayColor];
An extension for Swift 4 that provides functions to enable and disable the UISearchBar. Feel free to adjust the alpha value as you see fit, since color schemes in use can impact the result.
extension UISearchBar {
func enable() {
isUserInteractionEnabled = true
alpha = 1.0
}
func disable() {
isUserInteractionEnabled = false
alpha = 0.5
}
}
Then to disable the searchBar:
searchBar.disable()
To enable:
searchBar.enable()
For Swift 3:
self.searchController.searchBar.isUserInteractionEnabled = true
Or if you want to still detect user interaction you can do this to disable the UITextField inside the UISearchBar:
(self.searchController.searchBar.value(forKey: "searchField") as! UITextField).isEnabled = false
Really simply:
#IBOutlet weak var yourNameForSearchBar: UISearchBar!
viewDidLoad {
yourNameForSearchBar.isHidden = true
}
this makes it so the SearchBar just hides and user-touches in its spot do nothing. That way, when you want it shown, you just do the opposite (false). You can also save the space, but that's outside the scope of this answer.

Resources