Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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 UITextField没有最大字符限制的空格_Ios_Uitextfield_Whitespace_Uitextfielddelegate_Maxlength - Fatal编程技术网

Ios UITextField没有最大字符限制的空格

Ios UITextField没有最大字符限制的空格,ios,uitextfield,whitespace,uitextfielddelegate,maxlength,Ios,Uitextfield,Whitespace,Uitextfielddelegate,Maxlength,我有两个uitextfield:usernameField和passwordField。对于这两个字段,我试图实现下面的代码,以不允许用户键入任何空白(使用空格键),并限制每个文本字段的最大长度。空格的代码工作正常,但Max Length失败 //Addressing White space and length in the form - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang

我有两个uitextfield:usernameField和passwordField。对于这两个字段,我试图实现下面的代码,以不允许用户键入任何空白(使用空格键),并限制每个文本字段的最大长度。空格的代码工作正常,但Max Length失败

//Addressing White space and length in the form
- (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  {
    self.title.text = @"No Spaces Allowed";
    self.title.textColor = [UIColor yellowColor];
    return NO;
}


if (textField == self.usernameField)
{
    NSInteger MAXLENGTH = 15;

    NSUInteger oldLength = [textField.text length];
    NSUInteger replacementLength = [string length];
    NSUInteger rangeLength = range.length;

    NSUInteger newLength = oldLength - rangeLength + replacementLength;

    BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;

    return newLength <= MAXLENGTH || returnKey;

if (textField == self.passwordField)
    {
        NSInteger MAXLENGTH = 10;

        NSUInteger oldLength = [textField.text length];
        NSUInteger replacementLength = [string length];
        NSUInteger rangeLength = range.length;

        NSUInteger newLength = oldLength - rangeLength + replacementLength;

        BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;
        return newLength <= MAXLENGTH || returnKey;   
    }
}
}
//在表单中寻址空白和长度
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串{
NSString*resultingString=[textField.text StringByReplacingCharactersRange:range with string:string];
NSCharacterSet*whitespaceSet=[NSCharacterSet-whitespaceCharacterSet];
if([resultingString rangeOfCharacterFromSet:whitespaceSet].location==NSNotFound){
返回YES;
}否则{
self.title.text=@“不允许使用空格”;
self.title.textColor=[UIColor yellowColor];
返回否;
}
if(textField==self.usernameField)
{
NSInteger最大长度=15;
NSU整数oldLength=[textField.text length];
NSU整数替换长度=[字符串长度];
NSU整数rangeLength=range.length;
NSUInteger newLength=oldLength-rangeLength+replacementLength;
BOOL returnKey=[string rangeOfString:@“\n”]。位置!=NSNotFound;

返回newLength以允许最大字符数并拒绝空格

执行
应更改范围内的字符
委托

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL _isAllowed = YES;

    NSString *tempString = [[textField.text stringByReplacingCharactersInRange:range withString:string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    if ([self.yourTextFeildName.text isEqualToString:tempString] || [tempString length] > 5)
    {
        _isAllowed =  NO;
    }

    return   _isAllowed;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   if ([textField.text length] < 5)
   {
         //text field length is less than 5.
   }
}
用于最小字符数

执行
textfielddendediting
委托

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL _isAllowed = YES;

    NSString *tempString = [[textField.text stringByReplacingCharactersInRange:range withString:string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    if ([self.yourTextFeildName.text isEqualToString:tempString] || [tempString length] > 5)
    {
        _isAllowed =  NO;
    }

    return   _isAllowed;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   if ([textField.text length] < 5)
   {
         //text field length is less than 5.
   }
}
-(void)textfieldDendediting:(UITextField*)textField
{
如果([textField.text length]<5)
{
//文本字段长度小于5。
}
}