BTUIKMobileCountryCodeFormField.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #import "BTUIKMobileCountryCodeFormField.h"
  2. #import "BTUIKTextField.h"
  3. #import "BTUIKInputAccessoryToolbar.h"
  4. #import "BTUIKLocalizedString.h"
  5. @implementation BTUIKMobileCountryCodeFormField
  6. - (instancetype)initWithFrame:(CGRect)frame {
  7. self = [super initWithFrame:frame];
  8. if (self) {
  9. self.textField.accessibilityLabel = BTUIKLocalizedString(MOBILE_COUNTRY_CODE_LABEL);
  10. self.formLabel.text = BTUIKLocalizedString(MOBILE_COUNTRY_CODE_LABEL);
  11. self.textField.placeholder = @"+65";
  12. self.textField.keyboardType = UIKeyboardTypeNumberPad;
  13. }
  14. return self;
  15. }
  16. - (void)fieldContentDidChange {
  17. NSMutableString *s = [NSMutableString stringWithString:self.textField.text];
  18. NSUInteger slashLocation = [s rangeOfString:@"+"].location;
  19. if (slashLocation == NSNotFound && s.length > 0) {
  20. [s insertString:@"+" atIndex:0];
  21. } else if (s.length == 1) {
  22. s = [NSMutableString stringWithString:@""];
  23. }
  24. NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:s];
  25. self.textField.attributedText = result;
  26. [self updateAppearance];
  27. [self.delegate formFieldDidChange:self];
  28. }
  29. #pragma mark - Custom accessors
  30. - (BOOL)valid {
  31. return self.countryCode.length >= 1;
  32. }
  33. - (NSString *)countryCode {
  34. return [self.textField.text stringByReplacingOccurrencesOfString:@"+" withString:@""];
  35. }
  36. @end