uitextFiled字數輸入限制

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

    if (self.type == YXModifyTrueLoveInputTypeNick) {orm

        NSLog(@"%ld",[self countTheStrLength:[textField.text stringByReplacingCharactersInRange:range withString:string]]);ci

        NSInteger textLength = [self countTheStrLength:[textField.text stringByReplacingCharactersInRange:range withString:string]];string

 

        if (textLength > 4 && string.length > 0) {it

            return NO;coding

        } else {di

            self.textLabel.text = [NSString stringWithFormat:@"還能夠輸入%d個字符",(int)MAX(4 - textLength, 0)];co

        }字符

    }return

    return YES;

}

 

- ( NSInteger )countTheStrLength:( NSString *)strtemp {

    NSInteger strlength = 0 ;

    char * p = ( char *)[strtemp cStringUsingEncoding : NSUnicodeStringEncoding ];

    for ( int i= 0 ; i<[strtemp lengthOfBytesUsingEncoding : NSUnicodeStringEncoding ] ;i++) {

        if (*p) {

            p++;

            strlength++;

        }else {

            p++;

        }

    }

    return (strlength+ 1 )/ 2 ;

}

相關文章
相關標籤/搜索