Update textfieldview to be mftextfieldview

This commit is contained in:
Pfeil, Scott Robert 2019-01-30 11:38:11 -05:00
parent ddd5970546
commit 2924ed2cee
2 changed files with 80 additions and 9 deletions

View File

@ -7,9 +7,17 @@
//
#import <MVMCoreUI/ViewConstrainingView.h>
#import <MVMCoreUI/MFTextField.h>
@interface TextFieldView : ViewConstrainingView
@property (nonatomic, weak, nullable) UITextField *textField;
@property (nullable, weak, nonatomic) MFTextField *textField;
+ (nullable instancetype)createWithDelegate:(nullable id <UITextFieldDelegate, MFTextFieldDelegate>)delegate;
// Change the alignment of the label
- (void)alignLeft;
- (void)alignCenter;
- (void)alignRight;
@end

View File

@ -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 <UITextFieldDelegate, MFTextFieldDelegate>)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