Skip to content

Instantly share code, notes, and snippets.

Last active January 5, 2016 11:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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
@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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment