【问题标题】:Disable UITextField keyboard shortcut suggestions禁用 UITextField 键盘快捷键建议
【发布时间】:2014-05-15 09:25:09
【问题描述】:

有没有一种简单的方法可以从UITextField 中删除键盘快捷键建议

可以使用[textField setAutocorrectionType:UITextAutocorrectionTypeNo]; 删除打字更正,但这对快捷方式没有影响。

影响 sharedMenuController 也不会压缩它。

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

【问题讨论】:

    标签: ios ios7 uitextfield uitextview


    【解决方案1】:

    Matt 的解决方案转换为 Swift 5:

    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        // Pass through backspace and character input
        if (range.length > 0 && string.isEmpty) {
            textField.text?.removeLast()
        } else {
            textField.text?.append(string)
        }
        // Return false to override default UITextField behaviors
        return false
    }
    

    【讨论】:

      【解决方案2】:

      Objective-C

      textField.autocorrectionType = UITextAutocorrectionTypeNo;
      

      斯威夫特

      textField.autocorrectionType = .no
      

      文档 https://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html

      【讨论】:

        【解决方案3】:

        Swift 3.x 或更高版本:

        textField.autocorrectionType = .no
        

        【讨论】:

          【解决方案4】:

          使用自动更正类型:

          [mailTextField setAutocorrectionType:UITextAutocorrectionTypeNo];

          【讨论】:

            【解决方案5】:
            textField.autocorrectionType = .No
            

            【讨论】:

              【解决方案6】:

              上述答案可能不适用于剪切/复制/粘贴情况。例如在 UITextField 中剪切和粘贴文本时,结果与默认功能不同。

              下面是类似的方法:

              -(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]];
              }
              

              【讨论】:

                【解决方案7】:

                通过实现UITextFieldDelegate 方法并手动设置 UITextField 的 text 属性解决了这个问题。

                在模拟器中默认情况下,您可以通过键入 "omw" 来测试此行为,这应该会提示 “在我的路上!”。以下代码将阻止这一点。 注意:这也会禁用自动更正拼写检查,这在我的情况下是可以的。

                - (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;
                }
                

                【讨论】:

                • 不错!有什么新的默认解决方案吗?
                【解决方案8】:

                只用这个

                  textField.autocorrectionType = UITextAutocorrectionTypeNo;
                

                【讨论】:

                  【解决方案9】:
                  UITextField* f = [[UITextField alloc] init];
                  f.autocorrectionType = UITextAutocorrectionTypeNo;
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2012-11-06
                    • 2023-04-02
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2010-09-08
                    相关资源
                    最近更新 更多