如何知道 iOS 中的 UITextField 是否有空格

发布于 2024-12-17 14:22:49 字数 227 浏览 3 评论 0原文

我有一个 UITextField,用户可以在其中输入名称并保存。但是,不应允许用户在文本字段中输入空格。

1 - 我如何知道用户是否在textFiled中输入了两个空格或完整的空格

2 - 我如何知道textFiled是否仅填充了空格

编辑 - 只输入空格(空格)是无效的

I have a UITextField where user can enter a name and save it. But, user should not be allowed to enter blank spaces in the textFiled.

1 - How can I find out,if user has entered two blank spaces or complete blank spaces in the textFiled

2 - How can i know if the textFiled is filled only with blank spaces

edit - It is invalid to enter only white spaces(blank spaces)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

决绝 2024-12-24 14:22:49

您可以“修剪”文本,即删除开头和结尾处的所有空白。如果剩下的只是空字符串,则仅输入空格(或不输入任何内容)。

NSString *rawString = [textField text];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
if ([trimmed length] == 0) {
    // Text was empty or only whitespace.
}

如果您想检查是否有任何空格(文本中的任何位置),您可以这样做:

NSRange range = [rawString rangeOfCharacterFromSet:whitespace];
if (range.location != NSNotFound) {
    // There is whitespace.
}

如果您想完全阻止用户输入空格,请参阅@Hanon's解决方案。

You can "trim" the text, that is remove all the whitespace at the start and end. If all that's left is an empty string, then only whitespace (or nothing) was entered.

NSString *rawString = [textField text];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
if ([trimmed length] == 0) {
    // Text was empty or only whitespace.
}

If you want to check whether there is any whitespace (anywhere in the text), you can do it like this:

NSRange range = [rawString rangeOfCharacterFromSet:whitespace];
if (range.location != NSNotFound) {
    // There is whitespace.
}

If you want to prevent the user from entering whitespace at all, see @Hanon's solution.

暗恋未遂 2024-12-24 14:22:49

如果您确实想“限制”用户输入空格,

您可以在 UITextFieldDelegate 中实现以下方法

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

    NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
    NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
    if  ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound)      {
        return YES;
    }  else  {
        return NO;
    }
 }

如果用户在字段中输入空格,则当前文本不会发生变化

if you really want to 'restrict' user from entering white space

you can implement the following method in UITextFieldDelegate

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

    NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
    NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
    if  ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound)      {
        return YES;
    }  else  {
        return NO;
    }
 }

If user enter space in the field, there is no change in the current text

快乐很简单 2024-12-24 14:22:49

以下代码,请使用以下代码行

NSString *str_test = @"Example ";
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if([str_test rangeOfCharacterFromSet:whitespaceSet].location!=NSNotFound)
{
    NSLog(@"Found");
}

如果您想限制用户使用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string isEqualToString:@" "])
    {
        return NO
    }
    else
    {
        return YES
    }
}

Use following lines of code

NSString *str_test = @"Example ";
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if([str_test rangeOfCharacterFromSet:whitespaceSet].location!=NSNotFound)
{
    NSLog(@"Found");
}

if you want to restrict user use below code

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string isEqualToString:@" "])
    {
        return NO
    }
    else
    {
        return YES
    }
}
无妨# 2024-12-24 14:22:49

UPD: Swift 2.0 支持

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let whitespaceSet = NSCharacterSet.whitespaceCharacterSet()
    let range = string.rangeOfCharacterFromSet(whitespaceSet)
    if let _ = range {
        return false
    }
    else {
        return true
    }
}

UPD: Swift 2.0 Support

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let whitespaceSet = NSCharacterSet.whitespaceCharacterSet()
    let range = string.rangeOfCharacterFromSet(whitespaceSet)
    if let _ = range {
        return false
    }
    else {
        return true
    }
}
爺獨霸怡葒院 2024-12-24 14:22:49

我有同样的条件不允许用户输入空白字段

这是我的代码和检查语句

- (IBAction)acceptButtonClicked:(UIButton *)sender {
if ([self textFieldBlankorNot:fullNametext]) {
        fullNametext.text=@"na";
    }
// saving value to dictionary and sending to server

}

-(BOOL)textFieldBlankorNot:(UITextField *)textfield{
    NSString *rawString = [textfield text];
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
    if ([trimmed length] == 0)
        return YES;
    else
        return NO;
}

I had a same condition not allowing user to input blank field

Here is my code and check statement

- (IBAction)acceptButtonClicked:(UIButton *)sender {
if ([self textFieldBlankorNot:fullNametext]) {
        fullNametext.text=@"na";
    }
// saving value to dictionary and sending to server

}

-(BOOL)textFieldBlankorNot:(UITextField *)textfield{
    NSString *rawString = [textfield text];
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
    if ([trimmed length] == 0)
        return YES;
    else
        return NO;
}
情场扛把子 2024-12-24 14:22:49

这是 Swift 3 版本

let whitespaceSet = NSCharacterSet.whitespaces
let range = string.rangeOfCharacter(from: whitespaceSet)
if let _ = range {
    return false
}
else {
    return true
}

Heres Swift 3 version

let whitespaceSet = NSCharacterSet.whitespaces
let range = string.rangeOfCharacter(from: whitespaceSet)
if let _ = range {
    return false
}
else {
    return true
}
能否归途做我良人 2024-12-24 14:22:49

在 Swift 中,

如果你想限制用户,可以使用 contains()

例如,

if userTextField.text!.contains(" "){
//your code here.....
}

In Swift,

if you want to restrict the user, you can use contains()

For Example,

if userTextField.text!.contains(" "){
//your code here.....
}
老子叫无熙 2024-12-24 14:22:49

这是我使用 stringByReplacingOccurrencesOfString 所做的事情。

- (BOOL)validateFields
 {
      NSString *temp = [textField.text stringByReplacingOccurrencesOfString:@" "
                                                          withString:@""
                                                             options:NSLiteralSearch
                                                               range:NSMakeRange(0, textField.text.length)];

      if ([temp length] == 0) {
          // Alert view with message @"Please enter something."
          return NO;
      }
 }

Here's what I did using stringByReplacingOccurrencesOfString.

- (BOOL)validateFields
 {
      NSString *temp = [textField.text stringByReplacingOccurrencesOfString:@" "
                                                          withString:@""
                                                             options:NSLiteralSearch
                                                               range:NSMakeRange(0, textField.text.length)];

      if ([temp length] == 0) {
          // Alert view with message @"Please enter something."
          return NO;
      }
 }
冬天的雪花 2024-12-24 14:22:49

@Hanon 的答案非常简洁,但我需要的是至少允许 1 个空格,因此根据 Hanon 的解决方案,我做了这个:

我声明了一个名为 whitespaceCount 的局部变量来保留空格的计数。
希望这对任何人都有帮助!

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range     replacementString:(NSString *)string 
{ 
    NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];

    if ([string rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound) 
    {
        whitespaceCount++;
        if (whitespaceCount > 1) 
        {
            return NO;
        }
    }
    else 
    {
        whitespaceCount = 0;
        return YES;
    }
}

@Hanon's answer is the pretty neat, but what I needed was to allow at least 1 white space, so based on Hanon's solution I made this one:

I declared a local variable called whitespaceCount to keep the counts of the white spaces.
Hope this helps anybody!

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range     replacementString:(NSString *)string 
{ 
    NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];

    if ([string rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound) 
    {
        whitespaceCount++;
        if (whitespaceCount > 1) 
        {
            return NO;
        }
    }
    else 
    {
        whitespaceCount = 0;
        return YES;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文