iPhone SDK: disable auto creation of dot (.) in text field (or textview)

不羁的心 提交于 2019-12-18 06:54:57

问题


I've disabled auto-correction type for my text field, and it does not show any other auto-correction,

but it still automatically creates a dot (.) when I press space key twice.

For example, if I write "test" and press space key twice, it automatically changes into "test. "

Anyone knows how to disable this feature?

Thanks a lot.


回答1:


I found one solution - it uses UITextFieldTextDidChangeNotification because this occurs after the auto-correction applies.

  1. Set the delegate for the text field
  2. Set up a notification

    - (void) viewDidLoad {
    ...
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(textFieldDidChange:)
    name:UITextFieldTextDidChangeNotification object:tfTitle];
    }

  3. Then, notification handler
    - (void)textFieldDidChange:(NSNotification *)aNotification
    {
    if ( [textField.text rangeOfString:@". "].length ) {
    // Change text
    textField.text = [textField.text stringByReplacingOccurrencesOfString:@". " withString:@" "];
    }
    }




回答2:


Perhaps if you hook up a text field delegate and then implement the following method:

-(BOOL)shouldReplaceCharactersInRange:(NSRange)aRage withString:(NSString *)aString

You may be able to check aString for the autocorrected string (maybe @". ") and then just return NO. This will hopefully not allow the @" " to be replaced with @". "



来源:https://stackoverflow.com/questions/1528049/iphone-sdk-disable-auto-creation-of-dot-in-text-field-or-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!