Skip to content

Commit

Permalink
[TextControls] Expose density on text fields and text areas
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 355204832
  • Loading branch information
andrewoverton authored and material-automation committed Feb 2, 2021
1 parent 7df48ac commit 73e4048
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 5 deletions.
8 changes: 8 additions & 0 deletions components/TextControls/src/BaseTextAreas/MDCBaseTextArea.h
Expand Up @@ -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
7 changes: 6 additions & 1 deletion components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions components/TextControls/src/BaseTextFields/MDCBaseTextField.h
Expand Up @@ -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)
Expand Down
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -49,5 +49,6 @@
+ (void)configureTextFieldWithHebrewTextAndLeadingViewInRTL:(MDCBaseTextField *)textField;
+ (void)configureTextFieldWithLeadingViewTrailingViewAndCustomPaddings:
(MDCBaseTextField *)textField;
+ (void)configureEditingDenseTextFieldWithLabelTextAndText:(MDCBaseTextField *)textField;

@end
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down

0 comments on commit 73e4048

Please sign in to comment.