Skip to content

Instantly share code, notes, and snippets.

@gmarm
Last active January 5, 2016 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmarm/b2516bbd82284a31b9f1 to your computer and use it in GitHub Desktop.
Save gmarm/b2516bbd82284a31b9f1 to your computer and use it in GitHub Desktop.
STPPaymentCardTextField category to allow external value setting (implementation file)
//
// STPPaymentCardTextField+ExternalConfig.m
//
// Created by George Marmaridis on 18/12/15.
// Copyright © 2015 George Marmaridis. All rights reserved.
//
#import "STPPaymentCardTextField+ExternalConfig.h"
static NSString *const kNumberFieldKey = @"numberField";
static NSString *const kExpirationFieldKey = @"expirationField";
static NSString *const kCVCFieldKey = @"cvcField";
@interface STPPaymentCardTextField ()
typedef void (^STPNumberShrunkCompletionBlock)(BOOL completed);
- (BOOL)shouldShrinkNumberField;
- (void)setNumberFieldShrunk:(BOOL)shrunk animated:(BOOL)animated
completion:(STPNumberShrunkCompletionBlock)completion;
@end
@implementation STPPaymentCardTextField (ExternalConfig)
- (void)configureWithCardParams:(nonnull STPCardParams*)cardParams
{
if (cardParams.number.length)
{
[self p_setFieldWithName:kNumberFieldKey withValue:cardParams.number];
[self setNumberFieldShrunk:[self shouldShrinkNumberField] animated:NO completion:nil];
}
if (cardParams.expMonth && cardParams.expYear)
{
[self p_setFieldWithName:kExpirationFieldKey
withValue:[NSString stringWithFormat:@"%02lu/%02lu",
(unsigned long)cardParams.expMonth,
(unsigned long)cardParams.expYear%100]];
}
}
- (void)p_setFieldWithName:(nonnull NSString*)name withValue:(nonnull NSString*)value
{
UITextField *textField = (UITextField*)[self valueForKey:name];
id delegate = (id<UITextFieldDelegate>)self;
NSUInteger length = textField.text.length;
if ([delegate textField:textField shouldChangeCharactersInRange:NSMakeRange(0, length) replacementString:value])
{
textField.text = value;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment