2014-04-03 15 views
12

C'è un modo semplice per rimuovere suggerimenti di scelta rapida da tastiera da un UITextField?Disabilita UITextField suggerimenti di scelta rapida da tastiera

È possibile rimuovere la correzione di digitazione con: [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; tuttavia ciò non ha alcun effetto sui collegamenti.

Influenzare anche su sharedMenuController non lo schiaccia.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
    [UIMenuController sharedMenuController].menuVisible = NO; 
    return NO; 
} 

enter image description here

risposta

2

risolto questo implementando un metodo UITextFieldDelegate e impostare manualmente la proprietà di testo UITextField.

Per impostazione predefinita nel simulatore è possibile verificare questo comportamento digitando "omw" che dovrebbe suggerire "Sulla mia strada!". Il seguente codice bloccherà questo. Nota: disabilita anche la correzione automatica e ortografia di controllo che nel mio caso era ok.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    // Pass through backspace and character input 
    if (range.length > 0 && [string isEqualToString:@""]) { 
     textField.text = [textField.text substringToIndex:textField.text.length-1]; 
    } else { 
     textField.text = [textField.text stringByAppendingString:string]; 
    } 
    // Return NO to override default UITextField behaviors 
    return NO; 
} 
0
UITextField* f = [[UITextField alloc] init]; 
f.autocorrectionType = UITextAutocorrectionTypeNo; 
3

Usa questo solo

textField.autocorrectionType = UITextAutocorrectionTypeNo; 
0

La risposta di cui sopra potrebbe non funzionare con taglia/copia/incolla situazione. Ad esempio sul taglio e incolla del testo in UITextField, il risultato non è come per funzionalità predefinita.

Di seguito un approccio simile:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 

NSString *textFieldNewText = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

    if ([string isEqualToString:@""]) { 
     // return when something is being cut 
     return YES; 
    } 
    else 
    { 
     //set text field text 
     textField.text=textFieldNewText; 
     range.location=range.location+[string length]; 
     range.length=0; 
     [self selectTextInTextField:textField range:range]; 
    } 
    return NO; 
} 


//for handling cursor position when setting textfield text through code 
- (void)selectTextInTextField:(UITextField *)textField range:(NSRange)range { 

    UITextPosition *from = [textField positionFromPosition:[textField beginningOfDocument] offset:range.location]; 
    UITextPosition *to = [textField positionFromPosition:from offset:range.length]; 
    [textField setSelectedTextRange:[textField textRangeFromPosition:from toPosition:to]]; 
} 
1
textField.autocorrectionType = .No 
0

Uso AutocorrectionType:

[mailTextField setAutocorrectionType: UITextAutocorrectionTypeNo];

1

3.x Swift o superiore:

textField.autocorrectionType = .no 
Problemi correlati