13

I have a problem, when Right Aligning a UITextField on iOS7, when the user types "Space" it wont appear right away. If I type another character the spaces appears.

In iOS 6 does is not happening

http://www.blogosfera.co.uk/2013/10/ios-7-whitespace-not-visible-to-uitextfield-with-right-alignment/

Anyone know how to fix this?

apinho
  • 2,235
  • 3
  • 25
  • 39

2 Answers2

14
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location == textField.text.length && [string isEqualToString:@" "]) {
        // ignore replacement string and add your own
        textField.text = [textField.text stringByAppendingString:@"\u00a0"];
        return NO;
    }
    // for all other cases, proceed with replacement
    return YES;
}

To remove the code from text

self.txtFirstName.text = [self.txtFirstName.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];

FROM this stackoverflow answer - Right aligned UITextField spacebar does not advance cursor in iOS 7

Community
  • 1
  • 1
Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59
  • not working properly for me, I have to tap spacebar two times to start adding space at the end of string and same happens in case of backspace button to delete last space characters. – ViruMax Aug 11 '14 at 13:33
0

I don't know how to fix it but I have a suggestion, you could, in the mean time, replace all white spaces with a very similar unicode character (Like U+00A0), and then switch them back after another character has been typed?

Just include the <UITextFieldDelegate> in your .h, set the UITextField.delegate = self; in viewDidLoad

Then execute something like this:

 //!!!!!*****!*!*!!!*!*** Make SURE you set the delegate code before doing this (as mentioned in the original SO answer)

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@" "]) {
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"\u00A0"];//U+00A0 is a unicode character that seems very similar to space but isn't treated as whitespace...
    } else {
        textField.text = [textField.text stringByReplacingOccurrencesOfString:@"\u00A0" withString:@" "];//switches our u+00A0 unicode character back to a white-space everytime a space is not typed.
    }
    return (![string isEqualToString:@" "]);//returns YES if it doesn't equal whitespace, else NO (because we did a manual replace)
}

*Note, I haven't had a chance to test this out yet, as I'm not near an xCodeProj. Let me know how it works :) If anyone sees any errors please feel free to make an edit O:) Thanks All!

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • @AkshitZaveri perhaps Apple still interprets U+2004 as a space... try a different unicode character that resembles a space! – Albert Renshaw Jan 02 '14 at 19:51
  • Like U+3000 (Find and replace) – Albert Renshaw Jan 02 '14 at 19:52
  • Also make sure you set the delegate to "self" so that the code above actually gets triggered! Use an NSLog dec. to make sure it's being triggered :) – Albert Renshaw Jan 02 '14 at 19:53
  • I've used another solution (only the code is different) that worked.. i'll post it as an answer soon. :) – Akshit Zaveri Jan 03 '14 at 05:08
  • 1
    Yes, the code you posted is the same as this one (practically speaking), the U+00A0 is the correct unicode character! The only major differences in our code is that in mine it replaces the unicode character back with a space after it's done being typed, in yours it doesn't, they stay as U+00A0 forever, and in yours it only makes the change when the user is typing at the end which is probably best (Except if you are changing the U+00A0 back you need to do that even if they aren't typing at the end so it wouldn't have worked in mine... I'll edit in the new character to mine! – Albert Renshaw Jan 03 '14 at 06:28
  • Thanks for pointing that out. You can say that "I didn't tested it well enough". :D. But there's one problem with your code is that after typing space in middle of the text the cursor moves to the last position automatically !! While in the code that i posted it does not happen at all. – Akshit Zaveri Jan 03 '14 at 10:30
  • 1
    And another problem is : suppose you type "a" then space then "b". Now remove "b". The space disappears because u have replaced the code. So, it's actually better not to replace the code. Replace the code exactly before using the text from the textfield. – Akshit Zaveri Jan 03 '14 at 10:39
  • 1
    @AkshitZaveri Excellent points! Wow! :) You can fix both issues in my code... changing the range is easy, the backspace thing wouldn't be too hard either... but I'll just submit to the code you posted. – Albert Renshaw Jan 03 '14 at 16:20