From 2924ed2ceefbdcc4bae4000b28e8d31ea3ffed61 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 30 Jan 2019 11:38:11 -0500 Subject: [PATCH] Update textfieldview to be mftextfieldview --- MVMCoreUI/Atoms/Views/TextFieldView.h | 10 +++- MVMCoreUI/Atoms/Views/TextFieldView.m | 79 ++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/TextFieldView.h b/MVMCoreUI/Atoms/Views/TextFieldView.h index 5d87c301..0061e78d 100644 --- a/MVMCoreUI/Atoms/Views/TextFieldView.h +++ b/MVMCoreUI/Atoms/Views/TextFieldView.h @@ -7,9 +7,17 @@ // #import +#import @interface TextFieldView : ViewConstrainingView -@property (nonatomic, weak, nullable) UITextField *textField; +@property (nullable, weak, nonatomic) MFTextField *textField; + ++ (nullable instancetype)createWithDelegate:(nullable id )delegate; + +// Change the alignment of the label +- (void)alignLeft; +- (void)alignCenter; +- (void)alignRight; @end diff --git a/MVMCoreUI/Atoms/Views/TextFieldView.m b/MVMCoreUI/Atoms/Views/TextFieldView.m index a548b64d..e7c72c87 100644 --- a/MVMCoreUI/Atoms/Views/TextFieldView.m +++ b/MVMCoreUI/Atoms/Views/TextFieldView.m @@ -7,26 +7,36 @@ // #import "TextFieldView.h" +#import "NSLayoutConstraint+MFConvenience.h" + +@interface TextFieldView () +@property (weak, nonatomic) NSLayoutConstraint *alignCenterPin; +@property (weak, nonatomic) NSLayoutConstraint *alignCenterLeftPin; +@property (weak, nonatomic) NSLayoutConstraint *alignCenterRightPin; +@end @implementation TextFieldView ++ (instancetype)createWithDelegate:(id )delegate { + TextFieldView *textFieldView = [[TextFieldView alloc] init]; + textFieldView.textField.mfTextFieldDelegate = delegate; + textFieldView.textField.uiTextFieldDelegate = delegate; + return textFieldView; +} + - (void)setupView { + [super setupView]; if (!self.textField) { - self.backgroundColor = [UIColor clearColor]; - - UITextField *textField = [[UITextField alloc]initWithFrame:CGRectZero]; - [self addSubview:textField]; + MFTextField *textField = [MFTextField mfTextField]; textField.translatesAutoresizingMaskIntoConstraints = NO; + [self addSubview:textField]; self.textField = textField; - self.textField.backgroundColor = [UIColor whiteColor]; - // Align left and right constants. NSLayoutConstraint *leftPin = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]; self.leftPin = leftPin; leftPin.active = YES; NSLayoutConstraint *topPin = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; - topPin.priority = 999; self.topPin = topPin; topPin.active = YES; @@ -35,10 +45,63 @@ bottomPin.active = YES; NSLayoutConstraint *rightPin = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:textField attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; - rightPin.priority = 999; self.rightPin = rightPin; rightPin.active = YES; + + // Center alignments + NSLayoutConstraint *alignCenter = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; + self.alignCenterPin = alignCenter; + alignCenter.active = YES; + + NSLayoutConstraint *centerLeftPin = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]; + self.alignCenterLeftPin = centerLeftPin; + centerLeftPin.active = YES; + + NSLayoutConstraint *centerRightPin = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:textField attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; + self.alignCenterRightPin = centerRightPin; + centerRightPin.active = YES; + + [self alignLeft]; } } +- (void)alignLeft { + self.alignCenterPin.active = NO; + self.alignCenterLeftPin.active = NO; + self.alignCenterRightPin.active = NO; + self.leftPin.active = YES; + self.rightPin.active = YES; +} + +- (void)alignCenter { + self.alignCenterPin.active = YES; + self.alignCenterLeftPin.active = YES; + self.alignCenterRightPin.active = YES; + self.leftPin.active = NO; + self.rightPin.active = NO; +} + +- (void)alignRight { + self.alignCenterPin.active = NO; + self.alignCenterLeftPin.active = NO; + self.alignCenterRightPin.active = NO; + self.leftPin.active = YES; + self.rightPin.active = YES; +} + +- (void)setLeftPinConstant:(CGFloat)constant { + self.leftPin.constant = constant; + self.alignCenterLeftPin.constant = constant; +} + +- (void)setRightPinConstant:(CGFloat)constant { + self.rightPin.constant = constant; + self.alignCenterRightPin.constant = constant; +} + +- (void)resetConstraints { + [super resetConstraints]; + [self alignLeft]; +} + @end