Skip to content

Commit 2a31a31

Browse files
andrewovertonmaterial-automation
authored andcommitted
[TextControls] Disable label animation when label is truncated
PiperOrigin-RevId: 367256976
1 parent 32fdb16 commit 2a31a31

19 files changed

+142
-105
lines changed

components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ - (void)animateLabel {
489489
floatingLabelFrame:self.layout.labelFrameFloating
490490
normalFont:self.normalFont
491491
floatingFont:self.floatingFont
492+
labelTruncationIsPresent:self.layout.labelTruncationIsPresent
492493
animationDuration:self.animationDuration
493494
completion:^(BOOL finished) {
494495
if (finished) {

components/TextControls/src/BaseTextAreas/private/MDCBaseTextAreaLayout.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
@property(nonatomic, strong, nonnull, readonly) NSArray<NSNumber *> *verticalGradientLocations;
4444
@property(nonatomic, strong, nonnull, readonly) NSArray<NSNumber *> *horizontalGradientLocations;
4545

46+
@property(nonatomic, assign) BOOL labelTruncationIsPresent;
47+
4648
/**
4749
Initializing an MDCBaseTextAreaLayout object with this initializer is tantamount to calculating a
4850
layout for a text area. The long parameter list includes everything that might impact

components/TextControls/src/BaseTextAreas/private/MDCBaseTextAreaLayout.m

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616

1717
#import "MDCTextControlAssistiveLabelViewLayout.h"
1818
#import "MDCTextControlHorizontalPositioning.h"
19-
#import "MDCTextControlLabelPosition.h"
19+
#import "MDCTextControlLabelSupport.h"
2020
#import "MDCTextControlSideViewSupport.h"
2121
#import "MDCTextControlVerticalPositioningReference.h"
2222

23-
#import <MDFInternationalization/MDFInternationalization.h>
2423

2524
static const CGFloat kGradientBlurLength = (CGFloat)4.0;
2625

@@ -155,9 +154,23 @@ - (void)calculateLayoutWithSize:(CGSize)size
155154
CGFloat textRectMaxX = displaysRightView ? rightViewMinX - horizontalInterItemPadding
156155
: textAreaWidth - rightEdgePadding;
157156

157+
CGFloat textRectWidth = textRectMaxX - textRectMinX;
158+
CGSize labelSizeNormal = MDCTextControlLabelSizeWith(label.text, textRectWidth, font);
159+
CGSize labelSizeFloating = MDCTextControlLabelSizeWith(label.text, textRectWidth, floatingFont);
160+
BOOL normalLabelWillTruncate = labelSizeNormal.height > font.lineHeight;
161+
BOOL floatingLabelWillTruncate = labelSizeFloating.height > floatingFont.lineHeight;
162+
if (normalLabelWillTruncate) {
163+
labelSizeNormal.height = font.lineHeight;
164+
labelSizeNormal.width = textRectWidth;
165+
}
166+
if (floatingLabelWillTruncate) {
167+
labelSizeFloating.height = floatingFont.lineHeight;
168+
labelSizeFloating.width = textRectWidth;
169+
}
170+
self.labelTruncationIsPresent = normalLabelWillTruncate || floatingLabelWillTruncate;
171+
158172
CGRect floatingLabelFrame =
159-
[self floatingLabelFrameWithText:label.text
160-
floatingFont:floatingFont
173+
[self floatingLabelFrameWithFloatingLabelSize:labelSizeFloating
161174
textRectMinX:textRectMinX
162175
textRectMaxX:textRectMaxX
163176
paddingBetweenContainerTopAndFloatingLabel:verticalPositioningReference
@@ -166,8 +179,7 @@ - (void)calculateLayoutWithSize:(CGSize)size
166179
CGFloat floatingLabelMaxY = CGRectGetMaxY(floatingLabelFrame);
167180

168181
CGRect normalLabelFrame =
169-
[self normalLabelFrameWithLabelText:label.text
170-
font:font
182+
[self normalLabelFrameWithNormalLabelSize:labelSizeNormal
171183
textRectMinX:textRectMinX
172184
textRectMaxX:textRectMaxX
173185
paddingBetweenContainerTopAndNormalLabel:verticalPositioningReference
@@ -197,9 +209,8 @@ - (void)calculateLayoutWithSize:(CGSize)size
197209
containerHeight = verticalPositioningReference.containerHeightWithoutFloatingLabel;
198210
normalLabelFrame.origin.y = textViewMinY;
199211
}
200-
CGFloat textViewWidth = textRectMaxX - textRectMinX;
201212
CGFloat textViewHeight = containerHeight - bottomPadding - textViewMinY;
202-
CGRect textViewFrame = CGRectMake(textRectMinX, textViewMinY, textViewWidth, textViewHeight);
213+
CGRect textViewFrame = CGRectMake(textRectMinX, textViewMinY, textRectWidth, textViewHeight);
203214

204215
self.placeholderLabelHidden = placeholderLabel.attributedText == nil;
205216
if (self.placeholderLabelHidden) {
@@ -208,7 +219,7 @@ - (void)calculateLayoutWithSize:(CGSize)size
208219
CGSize sizeThatFits = [placeholderLabel sizeThatFits:textViewFrame.size];
209220
CGFloat placeholderLabelMinX = 0;
210221
if (isRTL) {
211-
placeholderLabelMinX = textViewWidth - sizeThatFits.width;
222+
placeholderLabelMinX = textRectWidth - sizeThatFits.width;
212223
}
213224
self.placeholderLabelFrame =
214225
CGRectMake(placeholderLabelMinX, 0, sizeThatFits.width, sizeThatFits.height);
@@ -304,14 +315,11 @@ - (CGRect)sideViewFrameWithSideView:(UIView *)sideView
304315
return CGRectMake(minX, minY, CGRectGetWidth(sideView.bounds), sideViewHeight);
305316
}
306317

307-
- (CGRect)normalLabelFrameWithLabelText:(NSString *)labelText
308-
font:(UIFont *)font
309-
textRectMinX:(CGFloat)textRectMinX
310-
textRectMaxX:(CGFloat)textRectMaxX
311-
paddingBetweenContainerTopAndNormalLabel:(CGFloat)paddingBetweenContainerTopAndNormalLabel
312-
isRTL:(BOOL)isRTL {
313-
CGFloat maxTextWidth = textRectMaxX - textRectMinX;
314-
CGSize normalLabelSize = [self textSizeWithText:labelText font:font maxWidth:maxTextWidth];
318+
- (CGRect)normalLabelFrameWithNormalLabelSize:(CGSize)normalLabelSize
319+
textRectMinX:(CGFloat)textRectMinX
320+
textRectMaxX:(CGFloat)textRectMaxX
321+
paddingBetweenContainerTopAndNormalLabel:(CGFloat)paddingBetweenContainerTopAndNormalLabel
322+
isRTL:(BOOL)isRTL {
315323
CGFloat normalLabelMinX = textRectMinX;
316324
if (isRTL) {
317325
normalLabelMinX = textRectMaxX - normalLabelSize.width;
@@ -321,14 +329,12 @@ - (CGRect)normalLabelFrameWithLabelText:(NSString *)labelText
321329
normalLabelSize.height);
322330
}
323331

324-
- (CGRect)floatingLabelFrameWithText:(NSString *)text
325-
floatingFont:(UIFont *)floatingFont
326-
textRectMinX:(CGFloat)textRectMinX
327-
textRectMaxX:(CGFloat)textRectMaxX
328-
paddingBetweenContainerTopAndFloatingLabel:(CGFloat)paddingBetweenContainerTopAndFloatingLabel
329-
isRTL:(BOOL)isRTL {
330-
CGFloat maxTextWidth = textRectMaxX - textRectMinX;
331-
CGSize floatingLabelSize = [self textSizeWithText:text font:floatingFont maxWidth:maxTextWidth];
332+
- (CGRect)floatingLabelFrameWithFloatingLabelSize:(CGSize)floatingLabelSize
333+
textRectMinX:(CGFloat)textRectMinX
334+
textRectMaxX:(CGFloat)textRectMaxX
335+
paddingBetweenContainerTopAndFloatingLabel:
336+
(CGFloat)paddingBetweenContainerTopAndFloatingLabel
337+
isRTL:(BOOL)isRTL {
332338
CGFloat textMinY = paddingBetweenContainerTopAndFloatingLabel;
333339
CGFloat textMinX = textRectMinX;
334340
if (isRTL) {

components/TextControls/src/BaseTextFields/MDCBaseTextField.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ - (void)animateLabel {
581581
floatingLabelFrame:self.layout.labelFrameFloating
582582
normalFont:self.normalFont
583583
floatingFont:self.floatingFont
584+
labelTruncationIsPresent:self.layout.labelTruncationIsPresent
584585
animationDuration:self.animationDuration
585586
completion:^(BOOL finished) {
586587
if (finished) {

components/TextControls/tests/snapshot/MDCBaseTextAreaSnapshotTests.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@ - (void)testDenseTextAreaWithLabelTextAndText {
213213
[self validateTextArea:textArea];
214214
}
215215

216+
- (void)testTextAreaWithTruncatingNormalLabel {
217+
// Given
218+
MDCBaseTextArea *textArea = self.textArea;
219+
220+
// When
221+
textArea.label.text = @"Very long label text withaverylongwordinit";
222+
223+
// Then
224+
[self validateTextArea:textArea];
225+
}
226+
216227
#pragma mark Helpers
217228

218229
- (UIView *)createSideView {

components/TextControls/tests/snapshot/MDCBaseTextFieldSnapshotTests.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#import "MaterialTextControls+BaseTextFields.h"
1516
#import "MaterialSnapshot.h"
1617

1718
#import <UIKit/UIKit.h>
@@ -280,4 +281,15 @@ - (void)testEditingDenseTextFieldWithLabelTextAndText {
280281
[self validateTextField:textField];
281282
}
282283

284+
- (void)testTextFieldWithTruncatingNormalLabel {
285+
// Given
286+
MDCBaseTextField *textField = self.textField;
287+
288+
// When
289+
textField.label.text = @"Very long label text withaverylongwordinit";
290+
291+
// Then
292+
[self validateTextField:textField];
293+
}
294+
283295
@end

components/private/TextControlsPrivate/src/Shared/MDCTextControl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import "MDCTextControlColorViewModel.h"
2222
#import "MDCTextControlHorizontalPositioningReference.h"
2323
#import "MDCTextControlLabelAnimation.h"
24-
#import "MDCTextControlLabelPosition.h"
24+
#import "MDCTextControlLabelSupport.h"
2525
#import "MDCTextControlVerticalPositioningReference.h"
2626

2727
UIFont *_Nonnull MDCTextControlDefaultUITextFieldFont(void);

components/private/TextControlsPrivate/src/Shared/MDCTextControlLabelAnimation.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#import <Foundation/Foundation.h>
1616
#import <UIKit/UIKit.h>
17-
#import "MDCTextControlLabelPosition.h"
17+
#import "MDCTextControlLabelSupport.h"
1818

1919
/**
2020
The logic to animate labels is extracted into its own class so that any MDCTextControl can
@@ -31,11 +31,12 @@
3131
no animation in progress.
3232
*/
3333
+ (void)animateLabel:(nonnull UILabel *)label
34-
state:(MDCTextControlLabelPosition)labelPosition
35-
normalLabelFrame:(CGRect)normalLabelFrame
36-
floatingLabelFrame:(CGRect)floatingLabelFrame
37-
normalFont:(nonnull UIFont *)normalFont
38-
floatingFont:(nonnull UIFont *)floatingFont
39-
animationDuration:(NSTimeInterval)animationDuration
40-
completion:(void (^__nullable)(BOOL))completion;
34+
state:(MDCTextControlLabelPosition)labelPosition
35+
normalLabelFrame:(CGRect)normalLabelFrame
36+
floatingLabelFrame:(CGRect)floatingLabelFrame
37+
normalFont:(nonnull UIFont *)normalFont
38+
floatingFont:(nonnull UIFont *)floatingFont
39+
labelTruncationIsPresent:(BOOL)labelTruncationIsPresent
40+
animationDuration:(NSTimeInterval)animationDuration
41+
completion:(void (^__nullable)(BOOL))completion;
4142
@end

components/private/TextControlsPrivate/src/Shared/MDCTextControlLabelAnimation.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
#import "MDCTextControlLabelAnimation.h"
1616

1717
#import "MaterialAnimationTiming.h"
18-
#import "MDCTextControlLabelPosition.h"
18+
#import "MDCTextControlLabelSupport.h"
1919

2020
@implementation MDCTextControlLabelAnimation
2121

2222
+ (void)animateLabel:(nonnull UILabel *)label
23-
state:(MDCTextControlLabelPosition)labelPosition
24-
normalLabelFrame:(CGRect)normalLabelFrame
25-
floatingLabelFrame:(CGRect)floatingLabelFrame
26-
normalFont:(nonnull UIFont *)normalFont
27-
floatingFont:(nonnull UIFont *)floatingFont
28-
animationDuration:(NSTimeInterval)animationDuration
29-
completion:(void (^__nullable)(BOOL))completion {
23+
state:(MDCTextControlLabelPosition)labelPosition
24+
normalLabelFrame:(CGRect)normalLabelFrame
25+
floatingLabelFrame:(CGRect)floatingLabelFrame
26+
normalFont:(nonnull UIFont *)normalFont
27+
floatingFont:(nonnull UIFont *)floatingFont
28+
labelTruncationIsPresent:(BOOL)labelTruncationIsPresent
29+
animationDuration:(NSTimeInterval)animationDuration
30+
completion:(void (^__nullable)(BOOL))completion {
3031
BOOL isAnimationInProgress = label.layer.animationKeys.count > 0;
3132
if (isAnimationInProgress) {
3233
if (completion) {
@@ -53,7 +54,7 @@ + (void)animateLabel:(nonnull UILabel *)label
5354
BOOL currentFrameIsCGRectZero = CGRectEqualToRect(currentFrame, CGRectZero);
5455
BOOL isNormalTransition = willChangeFrame && !currentFrameIsCGRectZero;
5556
BOOL nonZeroDuration = animationDuration > 0;
56-
BOOL shouldPerformAnimation = nonZeroDuration && isNormalTransition;
57+
BOOL shouldPerformAnimation = nonZeroDuration && isNormalTransition && !labelTruncationIsPresent;
5758

5859
void (^completionBlock)(BOOL finished) = ^void(BOOL finished) {
5960
label.transform = CGAffineTransformIdentity;

components/private/TextControlsPrivate/src/Shared/MDCTextControlLabelPosition.h renamed to components/private/TextControlsPrivate/src/Shared/MDCTextControlLabelSupport.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
#import <UIKit/UIKit.h>
1616

1717
/**
18-
This enum represents different states the floating label can be in.
18+
This enum represents different positional states the label can be in.
1919
*/
2020
typedef NS_ENUM(NSUInteger, MDCTextControlLabelPosition) {
2121
/**
22-
The state where the floating label is not visible.
22+
The state where the label is not visible.
2323
*/
2424
MDCTextControlLabelPositionNone,
2525
/**
26-
The state where the floating label is floating.
26+
The state where the label is positioned (i.e. floating) above the text rect.
2727
*/
2828
MDCTextControlLabelPositionFloating,
2929
/**
30-
The state where the floating label is occupying the normal text area.
30+
The state where the label is positioned in the normal text area.
3131
*/
3232
MDCTextControlLabelPositionNormal,
3333
};
3434

35-
MDCTextControlLabelPosition MDCTextControlLabelPositionWith(BOOL hasLabelText,
36-
BOOL hasText,
37-
BOOL canLabelFloat,
38-
BOOL isEditing);
35+
MDCTextControlLabelPosition MDCTextControlLabelPositionWith(BOOL hasLabelText, BOOL hasText,
36+
BOOL canLabelFloat, BOOL isEditing);
37+
38+
CGSize MDCTextControlLabelSizeWith(NSString *text, CGFloat maxWidth, UIFont *font);

0 commit comments

Comments
 (0)