Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Classes/NDEmailTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
*/
@property (null_resettable, weak, nonatomic) UIColor *domainTextColor;

/**
* The insets for the textView.
* If you set this property as nil, it will be reset to default inset.
* The default insets is UIEdgeInsetsMake(0, 0, 0, 0).
*/
@property (nonatomic, assign) UIEdgeInsets edgeInsets;

/**
Initialize and return `NDEmailTextField` with domain list.

Expand Down
17 changes: 16 additions & 1 deletion Classes/NDEmailTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (void)initialize
self.keyboardType = UIKeyboardTypeEmailAddress;
self.autocorrectionType = UITextAutocorrectionTypeNo;
self.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

[self addTarget:self
action:@selector(nd_emailEditingChanged:)
Expand Down Expand Up @@ -76,6 +77,11 @@ - (void)nd_emailEditingChanged:(id)sender
return;
}

CGFloat leftViewWidth = 0.0;
if(self.leftView) {
leftViewWidth = self.leftView.frame.size.width;
}

NSString *recommendDomain = [self.class recommentDomainWithText:self.text
inDomains:_domains];
NSString *writtenDomain = [self.text componentsSeparatedByString:@"@"].lastObject;
Expand All @@ -84,7 +90,7 @@ - (void)nd_emailEditingChanged:(id)sender
_domainLabel.text = domain;
CGFloat textWidth = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}].width;

_domainLabel.frame = CGRectMake(textWidth, .0f, CGRectGetWidth(self.frame) - textWidth, CGRectGetHeight(self.frame));
_domainLabel.frame = CGRectMake(textWidth + _edgeInsets.left + leftViewWidth, .0f, CGRectGetWidth(self.frame) - textWidth, CGRectGetHeight(self.frame));
}

- (void)nd_emailEditingEnded:(id)sender
Expand Down Expand Up @@ -159,6 +165,15 @@ + (NSString *)recommentDomainWithText:(NSString *)text

#pragma mark - View utility

- (CGRect)textRectForBounds:(CGRect)bounds {
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}


+ (UIColor *)defaultDomainTextColor
{
return [UIColor colorWithRed:191.f/255.f
Expand Down
12 changes: 12 additions & 0 deletions Demo/NDEmailTextFieldDemo/Controller/NDDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ - (void)viewDidLoad
self.view.backgroundColor = [UIColor whiteColor];

_defaultTextField = [[NDEmailTextField alloc] init];

_defaultTextField.placeholder = @"Basic text field";
_defaultTextField.layer.borderWidth = 1;

// If you want to apply inset

// _defaultTextField.leftView = leftView;
// UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, _defaultTextField.frame.size.height)];
// _defaultTextField.leftViewMode = UITextFieldViewModeAlways;

// or

_defaultTextField.edgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);

_customTextField = [[NDEmailTextField alloc] initWithDomains:@[@"mozzet.com"]];
_customTextField.font = [UIFont fontWithName:@"AmericanTypewriter-CondensedBold" size:20.f];
Expand Down