Skip to content

Commit 86241a2

Browse files
Nobodymaterial-automation
Nobody
authored andcommitted
Make textField padding and text inset an editable variable in chipField
PiperOrigin-RevId: 652493790
1 parent 1c85f89 commit 86241a2

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

components/Chips/src/MDCChipField.h

+13
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ typedef NS_OPTIONS(NSUInteger, MDCChipFieldDelimiter) {
135135
*/
136136
@property(nonatomic, assign) UIEdgeInsets contentEdgeInsets;
137137

138+
/**
139+
* The padding before the leading edge of the text field when there are chips. This is not applied
140+
* when there are no chips.
141+
* Default is 0.
142+
*/
143+
@property(nonatomic, assign) CGFloat textFieldLeadingPaddingWhenChipIsAdded;
144+
145+
/**
146+
* The insets of the text rect. Flipping for RTL is handled internally.
147+
* Default is 4.
148+
*/
149+
@property(nonatomic, assign) UIEdgeInsets textFieldTextInsets;
150+
138151
/**
139152
The image used in the MDCChipViewDeleteButton.
140153
Defaults to a bezier-drawn image declared in `MDCChipViewDeleteButton`.

components/Chips/src/MDCChipField.m

+18-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
static const CGFloat MDCChipFieldVerticalInset = 8;
3737
static const CGFloat MDCChipFieldHorizontalMargin = 8;
3838
static const CGFloat MDCChipFieldVerticalMargin = 8;
39+
static const UIEdgeInsets MDCChipFieldTextFieldTextInsetsDefault = {16, 4, 16, 0};
3940
static const UIKeyboardType MDCChipFieldDefaultKeyboardType = UIKeyboardTypeEmailAddress;
4041

4142
const CGFloat MDCChipFieldDefaultMinTextFieldWidth = 44;
@@ -53,20 +54,20 @@ @interface MDCChipFieldTextField : UITextField
5354

5455
@property(nonatomic, weak) id<MDCChipFieldTextFieldDelegate> deletionDelegate;
5556

57+
@property(nonatomic) UIEdgeInsets textFieldTextInsets;
58+
5659
@end
5760

5861
@implementation MDCChipFieldTextField
5962

60-
const UIEdgeInsets MDCChipFieldTextFieldRTLEdgeInsets = {16, 0, 16, 4};
61-
62-
const UIEdgeInsets MDCChipFieldTextFieldLTREdgeInsets = {16, 4, 16, 0};
63-
6463
- (BOOL)isRTL {
6564
return self.effectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
6665
}
6766

6867
- (UIEdgeInsets)textEdgeInsets {
69-
return [self isRTL] ? MDCChipFieldTextFieldRTLEdgeInsets : MDCChipFieldTextFieldLTREdgeInsets;
68+
return [self isRTL] ? UIEdgeInsetsMake(_textFieldTextInsets.top, _textFieldTextInsets.right,
69+
_textFieldTextInsets.bottom, _textFieldTextInsets.left)
70+
: _textFieldTextInsets;
7071
}
7172

7273
- (CGRect)textRectForBounds:(CGRect)bounds {
@@ -149,6 +150,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
149150
chipFieldTextField.autocorrectionType = UITextAutocorrectionTypeNo;
150151
chipFieldTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
151152
chipFieldTextField.keyboardType = MDCChipFieldDefaultKeyboardType;
153+
chipFieldTextField.textFieldTextInsets = MDCChipFieldTextFieldTextInsetsDefault;
152154
// Listen for notifications posted when the text field is the first responder.
153155
[[NSNotificationCenter defaultCenter] addObserver:self
154156
selector:@selector(textFieldDidChange)
@@ -182,6 +184,8 @@ - (void)commonMDCChipFieldInit {
182184
_contentEdgeInsets = MDCChipFieldDefaultContentEdgeInsets;
183185
_showPlaceholderWithChips = YES;
184186
_chipHeight = 32;
187+
_textFieldLeadingPaddingWhenChipIsAdded = 0;
188+
_textFieldTextInsets = MDCChipFieldTextFieldTextInsetsDefault;
185189

186190
[self configureLocalizedAccessibilityActionName];
187191
}
@@ -289,6 +293,13 @@ - (void)setPlaceholderAttributes:(NSDictionary<NSAttributedStringKey, id> *)plac
289293
[self updateTextFieldPlaceholderText];
290294
}
291295

296+
- (void)setTextFieldTextInsets:(UIEdgeInsets)textFieldTextInsets {
297+
_textFieldTextInsets = textFieldTextInsets;
298+
if ([_textField isKindOfClass:[MDCChipFieldTextField class]]) {
299+
((MDCChipFieldTextField *)self.textField).textFieldTextInsets = _textFieldTextInsets;
300+
}
301+
}
302+
292303
- (CGSize)sizeThatFits:(CGSize)size {
293304
NSArray *chipFrames = [self chipFramesForSize:size];
294305
CGRect lastChipFrame = [chipFrames.lastObject CGRectValue];
@@ -720,7 +731,8 @@ - (CGRect)frameForTextFieldForLastChipFrame:(CGRect)lastChipFrame
720731
chipFieldSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right;
721732
} else {
722733
// The text field fits on the line with chips
723-
originX += CGRectGetMaxX(lastChipFrame) + MDCChipFieldHorizontalMargin;
734+
originX += CGRectGetMaxX(lastChipFrame) + MDCChipFieldHorizontalMargin +
735+
_textFieldLeadingPaddingWhenChipIsAdded;
724736
textFieldWidth = availableWidth;
725737
}
726738

components/Chips/tests/snapshot/MDCChipFieldSnapshotTests.m

+27
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 <UIKit/UIKit.h>
1516
#import "MaterialChips.h"
1617
#import "MaterialSnapshot.h"
1718

@@ -123,6 +124,32 @@ - (void)testEmptyPlaceholderSizeDoesNotAccountForContentInsets {
123124
[self snapshotVerifyView:self.chipField];
124125
}
125126

127+
- (void)testTextFieldLeadingPaddingWhenChipIsAdded {
128+
// Given
129+
self.chipField.textField.text = @"This is a chip field.";
130+
self.chipField.textFieldLeadingPaddingWhenChipIsAdded = 50.0;
131+
132+
// When
133+
[self.chipField addChip:[self chipViewWithTitle:@"Chip"]];
134+
135+
// Then
136+
[self.chipField sizeToFit];
137+
[self snapshotVerifyView:self.chipField];
138+
}
139+
140+
- (void)testTextFieldTextLeadingInset {
141+
// Given
142+
self.chipField.textField.text = @"This is a chip field.";
143+
self.chipField.textFieldTextInsets = UIEdgeInsetsMake(16, 50, 16, 0);
144+
145+
// When
146+
[self.chipField addChip:[self chipViewWithTitle:@"Chip"]];
147+
148+
// Then
149+
[self.chipField sizeToFit];
150+
[self snapshotVerifyView:self.chipField];
151+
}
152+
126153
#pragma mark - Helpers
127154

128155
- (MDCChipView *)chipViewWithTitle:(NSString *)title {

0 commit comments

Comments
 (0)