I was able to get all of that working with a UITextField subclass. You're probably going to want to move those delegate methods into a ViewController and set the custom TextField delegate there. For the sake of this example it's easier to show you everything in one class. Obviously you're going to have to tweak those character sets allowed to meet you needs.
#import "TextField.h"
@interface TextField()<UITextFieldDelegate>
@end
@implementation TextField
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
}
return self;
}
- (UITextPosition *)closestPositionToPoint:(CGPoint)point{
UITextPosition *beginning = self.beginningOfDocument;
UITextPosition *end = [self positionFromPosition:beginning offset:self.text.length];
return end;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:)
|| action == @selector(select:)
|| action == @selector(selectAll:)
|| action == @selector(cut:)){
return NO;
}
return [super canPerformAction:action withSender:sender];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location == 0 && [string rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]].location == NSNotFound) {
return NO;
}else if (range.location == 1 && [string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == NSNotFound){
return NO;
}
return YES;
}
@end