Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何不允许键入';空间';在我看来?_Ios_Objective C_Text_Uitextview - Fatal编程技术网

Ios 如何不允许键入';空间';在我看来?

Ios 如何不允许键入';空间';在我看来?,ios,objective-c,text,uitextview,Ios,Objective C,Text,Uitextview,我有一个UITextView,我不希望用户在文本中键入任何空格。我该怎么做才能不让他使用空格键? 谢谢 我认为正确的方法是首先防止编辑: 在ViewController.h文件中,使其实现UITextViewDelegate协议: @interface ViewController : UIViewController <UITextViewDelegate> 最后,我们需要在更改发生时捕获更改并删除空格。我们可以在textViewDidChange:方法中执行此操作。当新字符串中

我有一个UITextView,我不希望用户在文本中键入任何空格。我该怎么做才能不让他使用空格键?
谢谢

我认为正确的方法是首先防止编辑:

在ViewController.h文件中,使其实现UITextViewDelegate协议:

@interface ViewController : UIViewController <UITextViewDelegate>
最后,我们需要在更改发生时捕获更改并删除空格。我们可以在textViewDidChange:方法中执行此操作。当新字符串中有空格时,在shouldChangeTextInRange:方法中返回NO将阻止用户粘贴其中有空格的文本(可能不是您想要的)。如果我们只是删除空格,用户无法从键盘键入新空格,但如果他们在剪贴板中使用“hello world”之类的内容进行粘贴,他们将在TextView中获得“helloworld”:

- (void)textViewDidChange:(UITextView *)textView
{
    // eliminates spaces, including those introduced by autocorrect
    if ([textView.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location != NSNotFound) {
        textView.text = [textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}

只是不允许使用链接中显示的方法使用空格aka@“(

记住设置文本视图的委托,您需要

  • 将视图控制器指定为文本视图的
    委托
    (可以通过编程或在Interface Builder中指定委托);以及

  • 您的
    UITextViewDelegate
    方法
    shouldChangeTextInRange
    需要检查要插入的字符串是否包含空格:

    或者,用Swift:

    extension ViewController: UITextViewDelegate {
        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            return text.rangeOfCharacter(from: .whitespacesAndNewlines) == nil
        }
    }
    
    请注意,这不是检查
    replacementText
    是否等于空格,因为这是一个不充分的检查。而是检查替换文本中是否有空格。这是一个重要的区别,因为可以将文本粘贴到文本视图中,该文本可能不等于空格,但不等于空格在粘贴的值中的某处包含空格

  • 只是不允许使用链接中显示的方法使用空格aka@。请记住设置文本视图的委托
    - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
     if( [string isEqualToString:@" "] )
        return NO;
     else 
        return YES;
    }
    
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
        if ([text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].location != NSNotFound) {
            return NO;
        }
        return YES;
    }
    
    extension ViewController: UITextViewDelegate {
        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            return text.rangeOfCharacter(from: .whitespacesAndNewlines) == nil
        }
    }