diff --git a/Classes/NDEmailTextField.h b/Classes/NDEmailTextField.h index c43fba1..b0a14b0 100644 --- a/Classes/NDEmailTextField.h +++ b/Classes/NDEmailTextField.h @@ -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. diff --git a/Classes/NDEmailTextField.m b/Classes/NDEmailTextField.m index e8ee6ea..e7cb4e2 100644 --- a/Classes/NDEmailTextField.m +++ b/Classes/NDEmailTextField.m @@ -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:) @@ -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; @@ -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 @@ -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 diff --git a/Demo/NDEmailTextFieldDemo/Controller/NDDemoViewController.m b/Demo/NDEmailTextFieldDemo/Controller/NDDemoViewController.m index e95bd46..dae263a 100644 --- a/Demo/NDEmailTextFieldDemo/Controller/NDDemoViewController.m +++ b/Demo/NDEmailTextFieldDemo/Controller/NDDemoViewController.m @@ -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];