Skip to content

Commit

Permalink
[TextFields] Pinning clear button to top of input. (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
willlarche authored Sep 19, 2017
1 parent 587ca2e commit 2149c01
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions components/TextFields/src/private/MDCTextInputCommonFundament.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ @interface MDCTextInputCommonFundament () {

@property(nonatomic, assign) BOOL isRegisteredForKVO;

@property(nonatomic, strong) NSLayoutConstraint *clearButtonCenterY;
@property(nonatomic, strong) NSLayoutConstraint *clearButtonTrailing;
@property(nonatomic, strong) NSLayoutConstraint *clearButtonWidth;
@property(nonatomic, strong) NSLayoutConstraint *leadingUnderlineLeading;
Expand Down Expand Up @@ -265,15 +266,19 @@ - (void)setupClearButton {
constant:MDCTextInputClearButtonImageSquareWidthHeight];
self.clearButtonWidth.priority = UILayoutPriorityDefaultLow;

NSLayoutConstraint *bottom = [NSLayoutConstraint
UIEdgeInsets insets = [self textInsets];
CGFloat scale = UIScreen.mainScreen.scale;
CGFloat centerYConstant = insets.top +
(MDCCeil(self.textInput.font.lineHeight * scale) / scale) / 2.f;
self.clearButtonCenterY = [NSLayoutConstraint
constraintWithItem:_clearButton
attribute:NSLayoutAttributeBottom
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.underline
toItem:_textInput
attribute:NSLayoutAttributeTop
multiplier:1
constant:-1 * MDCTextInputHalfPadding + MDCTextInputClearButtonImageBuiltInPadding];
bottom.priority = UILayoutPriorityDefaultLow;
constant:centerYConstant];
self.clearButtonCenterY.priority = UILayoutPriorityDefaultLow;

self.placeholderTrailing = [NSLayoutConstraint constraintWithItem:_placeholderLabel
attribute:NSLayoutAttributeTrailing
Expand All @@ -292,11 +297,12 @@ - (void)setupClearButton {
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:-1 *
(MDCTextInputClearButtonImageBuiltInPadding + [self textInsets].right)];
(MDCTextInputClearButtonImageBuiltInPadding + insets.right)];
self.clearButtonTrailing.priority = UILayoutPriorityDefaultLow;

[NSLayoutConstraint activateConstraints:@[
height, self.clearButtonWidth, bottom, self.placeholderLeading, self.clearButtonTrailing
height, self.clearButtonWidth, self.clearButtonCenterY, self.placeholderLeading,
self.clearButtonTrailing
]];

[_clearButton addTarget:self
Expand Down Expand Up @@ -535,8 +541,14 @@ - (void)updateClearButtonConstraints {
[self.textInput invalidateIntrinsicContentSize];
}

UIEdgeInsets insets = [self textInsets];

self.clearButtonTrailing.constant =
MDCTextInputClearButtonImageBuiltInPadding - [self textInsets].right;
MDCTextInputClearButtonImageBuiltInPadding - insets.right;
CGFloat scale = UIScreen.mainScreen.scale;
CGFloat centerYConstant = insets.top +
(MDCCeil(self.textInput.font.lineHeight * scale) / scale) / 2.f;
self.clearButtonCenterY.constant = centerYConstant;
}

- (CGFloat)clearButtonAlpha {
Expand Down

0 comments on commit 2149c01

Please sign in to comment.