Skip to content

Commit

Permalink
[TextFields] Fix text fields overlapping labels (#2163)
Browse files Browse the repository at this point in the history
* [TextFields] Removes ambiguous layout.

* [TextFields] Better code organization and comment.

* [TextFields] Adding a filled in field to the examples.

* [TextFields] Correcting placeholder not rising when text is set programatically. Also better naming.

* [TextFields] Giving examples of each floating style a set .text.

* [TextFields] Adding placeholder float test.
  • Loading branch information
willlarche committed Oct 13, 2017
1 parent d46e3a3 commit 75cf565
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions components/TextFields/examples/TextFieldExample.swift
Expand Up @@ -111,6 +111,7 @@ final class TextFieldSwiftExample: UIViewController {
scrollView.addSubview(name)
let nameController = MDCTextInputControllerDefault(textInput: name)
name.delegate = self
name.text = "Grace Hopper"
nameController.placeholderText = "Name"
nameController.helperText = "First and Last"
allTextFieldControllers.append(nameController)
Expand Down
Expand Up @@ -111,6 +111,7 @@ final class TextFieldFilledSwiftExample: UIViewController {
scrollView.addSubview(name)
let nameController = MDCTextInputControllerFilled(textInput: name)
name.delegate = self
name.text = "Grace Hopper"
nameController.placeholderText = "Name"
nameController.helperText = "First and Last"
allTextFieldControllers.append(nameController)
Expand Down
2 changes: 2 additions & 0 deletions components/TextFields/examples/TextFieldLegacyExample.swift
Expand Up @@ -109,7 +109,9 @@ final class TextFieldLegacySwiftExample: UIViewController {
scrollView.addSubview(name)
let nameController = MDCTextInputControllerLegacyDefault(textInput: name)
name.delegate = self
name.text = "Grace Hopper"
nameController.placeholderText = "Name"
nameController.helperText = "First and Last"
allTextFieldControllers.append(nameController)

scrollView.addSubview(address)
Expand Down
Expand Up @@ -108,6 +108,7 @@ final class TextFieldOutlinedSwiftExample: UIViewController {
scrollView.addSubview(name)
let nameController = MDCTextInputControllerOutlined(textInput: name)
name.delegate = self
name.text = "Grace Hopper"
nameController.placeholderText = "Name"
nameController.helperText = "First and Last"
allTextFieldControllers.append(nameController)
Expand Down
9 changes: 5 additions & 4 deletions components/TextFields/src/MDCTextInputControllerDefault.m
Expand Up @@ -344,7 +344,7 @@ - (void)commonMDCTextInputControllerDefaultInitialization {
_underlineViewMode = [self class].underlineViewModeDefault;
_textInput.hidesPlaceholderOnInput = NO;

[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
}

- (void)setupInput {
Expand Down Expand Up @@ -374,7 +374,7 @@ - (void)setupInput {

[self subscribeForNotifications];
_textInput.underline.color = [self class].normalColorDefault;
[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
}

- (void)subscribeForNotifications {
Expand Down Expand Up @@ -507,7 +507,7 @@ - (void)updatePlaceholder {
//
// Note that this calls updateLayout inside it so it is the only 'update-' method not included in
// updateLayout.
- (void)updatePlaceholderY {
- (void)forceUpdatePlaceholderY {
BOOL isDirectionToUp = NO;
if (self.floatingEnabled) {
isDirectionToUp = self.textInput.text.length >= 1 || self.textInput.isEditing;
Expand Down Expand Up @@ -885,7 +885,7 @@ + (void)setFloatingPlaceholderNormalColorDefault:(UIColor *)floatingPlaceholderN
- (void)setFloatingEnabled:(BOOL)floatingEnabled {
if (_floatingEnabled != floatingEnabled) {
_floatingEnabled = floatingEnabled;
[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
}
}

Expand Down Expand Up @@ -1312,6 +1312,7 @@ - (void)textInputDidChange:(NSNotification *)note {
[CATransaction begin];
[CATransaction setAnimationDuration:0];
[self updateLayout];
[self forceUpdatePlaceholderY];
[CATransaction commit];
} else {
[self updateLayout];
Expand Down
11 changes: 6 additions & 5 deletions components/TextFields/src/MDCTextInputControllerLegacyDefault.m
Expand Up @@ -317,7 +317,7 @@ - (void)commonMDCTextInputControllerLegacyDefaultInitialization {
_underlineViewMode = [self class].underlineViewModeDefault;
_textInput.hidesPlaceholderOnInput = NO;

[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
}

- (void)setupInput {
Expand All @@ -338,7 +338,7 @@ - (void)setupInput {

[self subscribeForNotifications];
_textInput.underline.color = [self class].normalColorDefault;
[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
}

- (void)setupClearButton {
Expand Down Expand Up @@ -481,7 +481,7 @@ - (void)updatePlaceholder {
//
// Note that this calls updateLayout inside it so it is the only 'update-' method not included in
// updateLayout.
- (void)updatePlaceholderY {
- (void)forceUpdatePlaceholderY {
BOOL isDirectionToUp = NO;
if (self.floatingEnabled) {
isDirectionToUp = self.textInput.text.length >= 1 || self.textInput.isEditing;
Expand Down Expand Up @@ -825,7 +825,7 @@ + (void)setFloatingPlaceholderNormalColorDefault:(UIColor *)floatingPlaceholderN
- (void)setFloatingEnabled:(BOOL)floatingEnabled {
if (_floatingEnabled != floatingEnabled) {
_floatingEnabled = floatingEnabled;
[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
}
}

Expand Down Expand Up @@ -1205,7 +1205,8 @@ - (void)textInputDidChange:(NSNotification *)note {
if ([note.name isEqualToString:MDCTextFieldTextDidSetTextNotification]) {
[CATransaction begin];
[CATransaction setAnimationDuration:0];
[self updatePlaceholderY];
[self forceUpdatePlaceholderY];
[self updateLayout];
[CATransaction commit];
} else {
[self updateLayout];
Expand Down
Expand Up @@ -236,6 +236,21 @@ class TextFieldControllerDefaultLegacyTests: XCTestCase {
XCTAssertNotEqual(.blue, textField.underline?.color)
}

func testFloatingPlaceholderLegacyDefault() {
let textField = MDCTextField()

let controller = MDCTextInputControllerLegacyDefault(textInput: textField)
textField.sizeToFit()

controller.placeholderText = "Placeholder"
textField.text = "Set Text"
textField.setNeedsLayout()
textField.layoutIfNeeded()

let estimatedTextFrame = UIEdgeInsetsInsetRect(textField.bounds, controller.textInsets(UIEdgeInsets()))
XCTAssertFalse(textField.placeholderLabel.frame.intersects(estimatedTextFrame))
}

func testLabelsLegacyDefault() {
let textField = MDCTextField()

Expand Down

0 comments on commit 75cf565

Please sign in to comment.