diff --git a/PaymentKit Example/PaymentKit Example.xcodeproj/project.pbxproj b/PaymentKit Example/PaymentKit Example.xcodeproj/project.pbxproj index 013ae42..0c64a6d 100644 --- a/PaymentKit Example/PaymentKit Example.xcodeproj/project.pbxproj +++ b/PaymentKit Example/PaymentKit Example.xcodeproj/project.pbxproj @@ -558,7 +558,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "PaymentKit Example/PaymentKit Example-Prefix.pch"; INFOPLIST_FILE = "PaymentKit Example/PaymentKit Example-Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 6.0; PRODUCT_NAME = "PaymentKit Example"; TARGETED_DEVICE_FAMILY = 1; WRAPPER_EXTENSION = app; @@ -571,7 +571,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "PaymentKit Example/PaymentKit Example-Prefix.pch"; INFOPLIST_FILE = "PaymentKit Example/PaymentKit Example-Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 6.0; PRODUCT_NAME = "PaymentKit Example"; TARGETED_DEVICE_FAMILY = 1; WRAPPER_EXTENSION = app; diff --git a/PaymentKit/PKView.m b/PaymentKit/PKView.m index 569691c..4f24d1a 100644 --- a/PaymentKit/PKView.m +++ b/PaymentKit/PKView.m @@ -84,13 +84,17 @@ - (void)setDefaultTextAttributes:(NSDictionary *)defaultTextAttributes // We shouldn't need to set the font and textColor attributes, but a bug exists in 7.0 (fixed in 7.1/) - NSArray *textFields = @[_cardNumberField, _cardExpiryField, _cardCVCField, _cardLastFourField]; - for (PKTextField *textField in textFields) { - textField.defaultTextAttributes = _defaultTextAttributes; - textField.font = _defaultTextAttributes[NSFontAttributeName]; - textField.textColor = _defaultTextAttributes[NSForegroundColorAttributeName]; - textField.textAlignment = NSTextAlignmentLeft; - } + if (_cardNumberField && _cardExpiryField && _cardLastFourField && _cardCVCField) { + NSArray *textFields = @[_cardNumberField, _cardExpiryField, _cardCVCField, _cardLastFourField]; + for (PKTextField *textField in textFields) { + if ([textField respondsToSelector:@selector(defaultTextAttributes)]) { + textField.defaultTextAttributes = _defaultTextAttributes; + } + textField.font = _defaultTextAttributes[NSFontAttributeName]; + textField.textColor = _defaultTextAttributes[NSForegroundColorAttributeName]; + textField.textAlignment = NSTextAlignmentLeft; + } + } _cardExpiryField.textAlignment = NSTextAlignmentCenter; _cardCVCField.textAlignment = NSTextAlignmentCenter; @@ -140,8 +144,12 @@ - (void)setup self.innerView.clipsToBounds = YES; _cardLastFourField = [UITextField new]; - _cardLastFourField.defaultTextAttributes = _defaultTextAttributes; + if ([_cardLastFourField respondsToSelector:@selector(defaultTextAttributes)]) { + _cardLastFourField.defaultTextAttributes = _defaultTextAttributes; + } _cardLastFourField.backgroundColor = self.backgroundColor; + _cardLastFourField.delegate = self; + _cardLastFourField.keyboardType = UIKeyboardTypeNumberPad; [self setupCardNumberField]; [self setupCardExpiryField]; @@ -172,7 +180,9 @@ - (PKTextField *)textFieldWithPlaceholder:(NSString *)placeholder textField.delegate = self; textField.placeholder = placeholder; textField.keyboardType = UIKeyboardTypeNumberPad; - textField.defaultTextAttributes = _defaultTextAttributes; + if ([textField respondsToSelector:@selector(defaultTextAttributes)]) { + textField.defaultTextAttributes = _defaultTextAttributes; + } textField.layer.masksToBounds = NO; return textField; @@ -242,26 +252,52 @@ - (void)layoutSubviews NSDictionary *attributes = self.defaultTextAttributes; - CGSize lastGroupSize, cvcSize, cardNumberSize; + CGSize lastGroupSize, cvcSize, cardNumberSize, expirySize; - if (self.cardNumber.cardType == PKCardTypeAmex) { - cardNumberSize = [@"1234 567890 12345" sizeWithAttributes:attributes]; - lastGroupSize = [@"00000" sizeWithAttributes:attributes]; - cvcSize = [@"0000" sizeWithAttributes:attributes]; - } - else { - if (self.cardNumber.cardType == PKCardTypeDinersClub) { - cardNumberSize = [@"1234 567890 1234" sizeWithAttributes:attributes]; - } - else { - cardNumberSize = [_cardNumberField.placeholder sizeWithAttributes:attributes]; - } - - lastGroupSize = [@"0000" sizeWithAttributes:attributes]; - cvcSize = [_cardCVCField.placeholder sizeWithAttributes:attributes]; - } + // Calculate size for iOS7 + if ([@"" respondsToSelector:@selector(sizeWithAttributes:)]) { + if (self.cardNumber.cardType == PKCardTypeAmex) { + cardNumberSize = [@"1234 567890 12345" sizeWithAttributes:attributes]; + lastGroupSize = [@"00000" sizeWithAttributes:attributes]; + cvcSize = [@"0000" sizeWithAttributes:attributes]; + } + else { + if (self.cardNumber.cardType == PKCardTypeDinersClub) { + cardNumberSize = [@"1234 567890 1234" sizeWithAttributes:attributes]; + } + else { + cardNumberSize = [_cardNumberField.placeholder sizeWithAttributes:attributes]; + } + + lastGroupSize = [@"0000" sizeWithAttributes:attributes]; + cvcSize = [_cardCVCField.placeholder sizeWithAttributes:attributes]; + } + + expirySize = [_cardExpiryField.placeholder sizeWithAttributes:attributes]; + } + + // Calculate size for iOS6 + else { + if (self.cardNumber.cardType == PKCardTypeAmex) { + cardNumberSize = [@"1234 567890 12345" sizeWithFont:self.font]; + lastGroupSize = [@"00000" sizeWithFont:self.font]; + cvcSize = [@"0000" sizeWithFont:self.font]; + } + else { + if (self.cardNumber.cardType == PKCardTypeDinersClub) { + cardNumberSize = [@"1234 567890 1234" sizeWithFont:self.font]; + } + else { + cardNumberSize = [_cardNumberField.placeholder sizeWithFont:self.font]; + } + + lastGroupSize = [@"0000" sizeWithFont:self.font]; + cvcSize = [_cardCVCField.placeholder sizeWithFont:self.font]; + } + + expirySize = [_cardExpiryField.placeholder sizeWithFont:self.font]; + } - CGSize expirySize = [_cardExpiryField.placeholder sizeWithAttributes:attributes]; CGFloat textFieldY = (self.frame.size.height - lastGroupSize.height) / 2.0; @@ -512,8 +548,9 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField [self setPlaceholderToCardType]; } - if ([textField isEqual:_cardNumberField] && !isInitialState) { + if ([textField isEqual:_cardLastFourField] && !isInitialState) { [self stateCardNumber]; + [_cardNumberField becomeFirstResponder]; } } @@ -549,6 +586,7 @@ - (BOOL)cardNumberFieldShouldChangeCharactersInRange:(NSRange)range replacementS PKCardNumber *cardNumber = [PKCardNumber cardNumberWithString:resultString]; if (![cardNumber isPartiallyValid]) { + [self stateMeta]; return NO; }