From 73e404888d1e98e930e62b68f864b60e264c32f3 Mon Sep 17 00:00:00 2001 From: Andrew Overton Date: Tue, 2 Feb 2021 10:55:10 -0800 Subject: [PATCH] [TextControls] Expose density on text fields and text areas PiperOrigin-RevId: 355204832 --- .../src/BaseTextAreas/MDCBaseTextArea.h | 8 ++++++++ .../src/BaseTextAreas/MDCBaseTextArea.m | 7 ++++++- .../src/BaseTextFields/MDCBaseTextField.h | 8 ++++++++ .../src/BaseTextFields/MDCBaseTextField.m | 7 ++++++- .../tests/snapshot/MDCBaseTextAreaSnapshotTests.m | 14 ++++++++++++++ .../tests/snapshot/MDCBaseTextFieldSnapshotTests.m | 12 ++++++++++++ .../snapshot/MDCFilledTextFieldSnapshotTests.m | 12 ++++++++++++ .../snapshot/MDCOutlinedTextFieldSnapshotTests.m | 12 ++++++++++++ .../MDCBaseTextFieldTestsSnapshotTestHelpers.h | 1 + .../MDCBaseTextFieldTestsSnapshotTestHelpers.m | 7 +++++++ ...TextControlVerticalPositioningReferenceFilled.m | 2 +- ...xtControlVerticalPositioningReferenceOutlined.m | 4 ++-- 12 files changed, 89 insertions(+), 5 deletions(-) diff --git a/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.h b/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.h index b33195f8ecb..54c0b022ffd 100644 --- a/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.h +++ b/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.h @@ -229,4 +229,12 @@ values are allowed. */ @property(nonatomic, strong, null_resettable) UIColor *placeholderColor; +/** + This property influences how much vertical space there is between the various elements contained in + the text area. When it's 0 (the default) there is maximal vertical space between elements. When it + is 1, there is minimal vertical space between elements. Values less than 0 and greater than 1 will + be clamped. + */ +@property(nonatomic, assign) CGFloat verticalDensity; + @end diff --git a/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m b/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m index e20c713a87c..b9f3b933227 100644 --- a/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m +++ b/components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m @@ -293,7 +293,7 @@ - (MDCBaseTextAreaLayout *)calculateLayoutWithSize:(CGSize)size { textRowHeight:(self.normalFont.lineHeight + self.normalFont.leading) numberOfTextRows:self.numberOfLinesOfVisibleText - density:0 + density:self.verticalDensity preferredContainerHeight:self.preferredContainerHeight isMultilineTextControl:YES]; } @@ -537,6 +537,11 @@ - (void)setPlaceholderColor:(UIColor *)placeholderColor { _placeholderColor = placeholderColor ?: [self defaultPlaceholderColor]; } +- (void)setVerticalDensity:(CGFloat)verticalDensity { + _verticalDensity = verticalDensity; + [self setNeedsLayout]; +} + #pragma mark MDCTextControl Protocol Accessors - (void)setLeadingView:(UIView *)leadingView { diff --git a/components/TextControls/src/BaseTextFields/MDCBaseTextField.h b/components/TextControls/src/BaseTextFields/MDCBaseTextField.h index e4000a68d25..d2e411444b4 100644 --- a/components/TextControls/src/BaseTextFields/MDCBaseTextField.h +++ b/components/TextControls/src/BaseTextFields/MDCBaseTextField.h @@ -205,6 +205,14 @@ */ @property(nonatomic, assign) CGFloat containerRadius; +/** + This property influences how much vertical space there is between the various elements contained in + the text field. When it's 0 (the default) there is maximal vertical space between elements. When it + is 1, there is minimal vertical space between elements. Values less than 0 and greater than 1 will + be clamped. + */ +@property(nonatomic, assign) CGFloat verticalDensity; + @end @interface MDCBaseTextField (UIAccessibility) diff --git a/components/TextControls/src/BaseTextFields/MDCBaseTextField.m b/components/TextControls/src/BaseTextFields/MDCBaseTextField.m index 4d279a152b2..1a227c53549 100644 --- a/components/TextControls/src/BaseTextFields/MDCBaseTextField.m +++ b/components/TextControls/src/BaseTextFields/MDCBaseTextField.m @@ -327,7 +327,7 @@ - (MDCBaseTextFieldLayout *)calculateLayoutWithTextFieldSize:(CGSize)textFieldSi normalFontLineHeight:self.normalFont.lineHeight textRowHeight:self.normalFont.lineHeight numberOfTextRows:self.numberOfLinesOfVisibleText - density:0 + density:self.verticalDensity preferredContainerHeight:self.preferredContainerHeight isMultilineTextControl:NO]; } @@ -457,6 +457,11 @@ - (void)setLeadingViewMode:(UITextFieldViewMode)leadingViewMode { [self setNeedsLayout]; } +- (void)setVerticalDensity:(CGFloat)verticalDensity { + _verticalDensity = verticalDensity; + [self setNeedsLayout]; +} + #pragma mark MDCTextControl accessors - (void)setLabelBehavior:(MDCTextControlLabelBehavior)labelBehavior { diff --git a/components/TextControls/tests/snapshot/MDCBaseTextAreaSnapshotTests.m b/components/TextControls/tests/snapshot/MDCBaseTextAreaSnapshotTests.m index 8a2de87bd4e..de75961c199 100644 --- a/components/TextControls/tests/snapshot/MDCBaseTextAreaSnapshotTests.m +++ b/components/TextControls/tests/snapshot/MDCBaseTextAreaSnapshotTests.m @@ -199,6 +199,20 @@ - (void)testTextAreaWithLeadingViewTrailingViewAndCustomPaddings { [self validateTextArea:textArea]; } +- (void)testDenseTextAreaWithLabelTextAndText { + // Given + MDCBaseTextArea *textArea = self.textArea; + + // When + textArea.label.text = @"Label text"; + textArea.textView.text = @"Text"; + textArea.verticalDensity = 1.0f; + [textArea.textView becomeFirstResponder]; + + // Then + [self validateTextArea:textArea]; +} + #pragma mark Helpers - (UIView *)createSideView { diff --git a/components/TextControls/tests/snapshot/MDCBaseTextFieldSnapshotTests.m b/components/TextControls/tests/snapshot/MDCBaseTextFieldSnapshotTests.m index c415460b214..e35bfa5594a 100644 --- a/components/TextControls/tests/snapshot/MDCBaseTextFieldSnapshotTests.m +++ b/components/TextControls/tests/snapshot/MDCBaseTextFieldSnapshotTests.m @@ -268,4 +268,16 @@ - (void)testTextFieldWithLeadingViewTrailingViewAndCustomPaddings { [self validateTextField:textField]; } +- (void)testEditingDenseTextFieldWithLabelTextAndText { + // Given + MDCBaseTextField *textField = self.textField; + + // When + [MDCBaseTextFieldTestsSnapshotTestHelpers + configureEditingDenseTextFieldWithLabelTextAndText:textField]; + + // Then + [self validateTextField:textField]; +} + @end diff --git a/components/TextControls/tests/snapshot/MDCFilledTextFieldSnapshotTests.m b/components/TextControls/tests/snapshot/MDCFilledTextFieldSnapshotTests.m index 17a44e55d61..b030a706a7e 100644 --- a/components/TextControls/tests/snapshot/MDCFilledTextFieldSnapshotTests.m +++ b/components/TextControls/tests/snapshot/MDCFilledTextFieldSnapshotTests.m @@ -269,4 +269,16 @@ - (void)testTextFieldWithLeadingViewTrailingViewAndCustomPaddings { [self validateTextField:textField]; } +- (void)testEditingDenseTextFieldWithLabelTextAndText { + // Given + MDCFilledTextField *textField = self.textField; + + // When + [MDCBaseTextFieldTestsSnapshotTestHelpers + configureEditingDenseTextFieldWithLabelTextAndText:textField]; + + // Then + [self validateTextField:textField]; +} + @end diff --git a/components/TextControls/tests/snapshot/MDCOutlinedTextFieldSnapshotTests.m b/components/TextControls/tests/snapshot/MDCOutlinedTextFieldSnapshotTests.m index 3ed7a13a73f..fbaa8003dbd 100644 --- a/components/TextControls/tests/snapshot/MDCOutlinedTextFieldSnapshotTests.m +++ b/components/TextControls/tests/snapshot/MDCOutlinedTextFieldSnapshotTests.m @@ -269,4 +269,16 @@ - (void)testTextFieldWithLeadingViewTrailingViewAndCustomPaddings { [self validateTextField:textField]; } +- (void)testEditingDenseTextFieldWithLabelTextAndText { + // Given + MDCOutlinedTextField *textField = self.textField; + + // When + [MDCBaseTextFieldTestsSnapshotTestHelpers + configureEditingDenseTextFieldWithLabelTextAndText:textField]; + + // Then + [self validateTextField:textField]; +} + @end diff --git a/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.h b/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.h index e7cce49b201..901a368c304 100644 --- a/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.h +++ b/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.h @@ -49,5 +49,6 @@ + (void)configureTextFieldWithHebrewTextAndLeadingViewInRTL:(MDCBaseTextField *)textField; + (void)configureTextFieldWithLeadingViewTrailingViewAndCustomPaddings: (MDCBaseTextField *)textField; ++ (void)configureEditingDenseTextFieldWithLabelTextAndText:(MDCBaseTextField *)textField; @end diff --git a/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.m b/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.m index 282626ec766..e76e7c2faa6 100644 --- a/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.m +++ b/components/TextControls/tests/snapshot/supplemental/MDCBaseTextFieldTestsSnapshotTestHelpers.m @@ -237,6 +237,13 @@ + (void)configureTextFieldWithLeadingViewTrailingViewAndCustomPaddings: textField.horizontalInterItemSpacingOverride = @(30.0f); } ++ (void)configureEditingDenseTextFieldWithLabelTextAndText:(MDCBaseTextField *)textField { + textField.text = @"Some text"; + textField.label.text = @"Label text"; + textField.verticalDensity = 1.0f; + [textField becomeFirstResponder]; +} + #pragma mark Helpers + (UIView *)createBlueSideView { diff --git a/components/private/TextControlsPrivate/src/FilledStyle/MDCTextControlVerticalPositioningReferenceFilled.m b/components/private/TextControlsPrivate/src/FilledStyle/MDCTextControlVerticalPositioningReferenceFilled.m index d24b311b23c..ac42a989ff3 100644 --- a/components/private/TextControlsPrivate/src/FilledStyle/MDCTextControlVerticalPositioningReferenceFilled.m +++ b/components/private/TextControlsPrivate/src/FilledStyle/MDCTextControlVerticalPositioningReferenceFilled.m @@ -17,7 +17,7 @@ static const CGFloat kMinPaddingAroundTextWhenNoFloatingLabel = 6.0f; static const CGFloat kMaxPaddingAroundTextWhenNoFloatingLabel = 10.0f; -static const CGFloat kMinPaddingBetweenContainerTopAndFloatingLabel = 6.0f; +static const CGFloat kMinPaddingBetweenContainerTopAndFloatingLabel = 3.0f; static const CGFloat kMaxPaddingBetweenContainerTopAndFloatingLabel = 10.0f; static const CGFloat kMinPaddingBetweenFloatingLabelAndEditingText = 3.0f; static const CGFloat kMaxPaddingBetweenFloatingLabelAndEditingText = 6.0f; diff --git a/components/private/TextControlsPrivate/src/OutlinedStyle/MDCTextControlVerticalPositioningReferenceOutlined.m b/components/private/TextControlsPrivate/src/OutlinedStyle/MDCTextControlVerticalPositioningReferenceOutlined.m index 0504659cf11..974361d9af2 100644 --- a/components/private/TextControlsPrivate/src/OutlinedStyle/MDCTextControlVerticalPositioningReferenceOutlined.m +++ b/components/private/TextControlsPrivate/src/OutlinedStyle/MDCTextControlVerticalPositioningReferenceOutlined.m @@ -20,9 +20,9 @@ achieve the look and feel of the textfields at https://material.io/design/components/text-fields.html. */ -static const CGFloat kMinPaddingAroundTextWhenNoFloatingLabel = 6.0f; +static const CGFloat kMinPaddingAroundTextWhenNoFloatingLabel = 4.0f; static const CGFloat kMaxPaddingAroundTextWhenNoFloatingLabel = 10.0f; -static const CGFloat kMinPaddingBetweenFloatingLabelAndEditingText = (CGFloat)8.0; +static const CGFloat kMinPaddingBetweenFloatingLabelAndEditingText = (CGFloat)3.0; static const CGFloat kMaxPaddingBetweenFloatingLabelAndEditingText = (CGFloat)12.0; static const CGFloat kMinPaddingAroundAssistiveLabels = (CGFloat)3.0; static const CGFloat kMaxPaddingAroundAssistiveLabels = (CGFloat)6.0;