Skip to content

Commit

Permalink
[TextFields] Implement some UIView overrides in MDCBaseTextField (mat…
Browse files Browse the repository at this point in the history
…erial-components#8494)

This PR adds a bit some UIView overrides to MDCBaseTextField with some additional minor cleanup and wiring, and a disabled snapshot test :D

Related to #6942.
  • Loading branch information
andrewoverton authored Sep 25, 2019
1 parent 2f4b35e commit e1c4832
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
23 changes: 23 additions & 0 deletions components/TextFields/src/ContainedInputView/MDCBaseTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,19 @@ - (void)layoutSubviews {
[self postLayoutSubviews];
}

// UITextField's sizeToFit calls this method and then also calls setNeedsLayout.
// When the system calls this method the size parameter is the view's current size.
- (CGSize)sizeThatFits:(CGSize)size {
return [self preferredSizeWithWidth:size.width];
}

- (CGSize)intrinsicContentSize {
return [self preferredSizeWithWidth:CGRectGetWidth(self.bounds)];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];

self.layoutDirection = self.mdf_effectiveUserInterfaceLayoutDirection;
}

Expand Down Expand Up @@ -193,8 +204,20 @@ - (CGFloat)clearButtonSideLengthWithTextFieldSize:(CGSize)textFieldSize {
return systemPlaceholderRect.size.height;
}

- (CGSize)preferredSizeWithWidth:(CGFloat)width {
CGSize fittingSize = CGSizeMake(width, CGFLOAT_MAX);
MDCBaseTextFieldLayout *layout = [self calculateLayoutWithTextFieldSize:fittingSize];
return CGSizeMake(width, layout.calculatedHeight);
}

#pragma mark UITextField Accessor Overrides

- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];

[self setNeedsLayout];
}

- (void)setLeftViewMode:(UITextFieldViewMode)leftViewMode {
NSLog(@"Setting leftViewMode is not recommended. Consider setting leadingViewMode and "
@"trailingViewMode instead.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
@property(nonatomic, assign) CGRect leftViewFrame;
@property(nonatomic, assign) CGRect rightViewFrame;

@property(nonatomic, readonly) CGFloat calculatedHeight;
@property(nonatomic, assign) CGFloat containerHeight;

- (nonnull instancetype)initWithTextFieldSize:(CGSize)textFieldSize
positioningReference:
(nonnull id<MDCContainerStyleVerticalPositioningReference>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ - (void)calculateLayoutWithTextFieldSize:(CGSize)textFieldSize
self.labelFrameNormal = labelFrameNormal;
self.leftViewHidden = !displaysLeftView;
self.rightViewHidden = !displaysRightView;
self.containerHeight = positioningReference.containerHeight;
}

- (CGFloat)minYForSubviewWithHeight:(CGFloat)height centerY:(CGFloat)centerY {
Expand Down Expand Up @@ -342,4 +343,8 @@ - (CGFloat)textHeightWithFont:(UIFont *)font {
return (CGFloat)ceil((double)font.lineHeight);
}

- (CGFloat)calculatedHeight {
return self.containerHeight;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,16 @@ - (void)testFloatingLabelWithCustomColorWhileEditing {
[self validateTextField:textField];
}

- (void)testDisabledTextField {
// Given
MDCBaseTextField *textField = self.textField;

// When
textField.label.text = @"Floating label text";
textField.text = @"Text";
textField.enabled = NO;

// Then
[self validateTextField:textField];
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,19 @@ - (void)testTextColorAccessors {
[textField textColorForState:MDCTextControlStateDisabled]);
}

- (void)testSizeThatFits {
// Given
CGRect largeTextFieldFrame = CGRectMake(0, 0, 130, 300);
MDCBaseTextField *textField = [[MDCBaseTextField alloc] initWithFrame:largeTextFieldFrame];

// When
textField.text = @"text";
[textField sizeToFit];

// Then
CGSize newSize = textField.frame.size;
CGSize correctSize = CGSizeMake(130, 50);
XCTAssertTrue(CGSizeEqualToSize(newSize, correctSize));
}

@end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e1c4832

Please sign in to comment.