iOS 限制UITextField输入文本长度

  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:alcoholConsumptionTF] ) {
    if (textField.text.length>=5) {
    NSString *string = [textField.text substringToIndex:5];//截取范围内的字符串
    textField.text=string;
    return NO;
    }
    }
    }

你可能感兴趣的:(iOS 限制UITextField输入文本长度)