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

亡梦爱人 提交于 2019-11-29 11:22:05

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:@" "];
    }
    }

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 @". "

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