Skip to content

Commit 448d787

Browse files
committed
Fix textPadding for left & right views
1 parent ec29724 commit 448d787

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

MaterialTextField/MFTextField.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,16 @@ - (void)removeErrorLabel
642642
- (CGRect)textRectForBounds:(CGRect)bounds
643643
{
644644
CGRect superRect = [super textRectForBounds:bounds];
645-
CGRect rect = CGRectMake(superRect.origin.x + self.textPadding.width,
645+
CGRect rightRect = [super rightViewRectForBounds:bounds];
646+
BOOL hasLeftView = [self leftViewRectForBounds:bounds].size.width > 0;
647+
BOOL hasRightView = [self rightViewRectForBounds:bounds].size.width > 0;
648+
649+
CGFloat leftPadding = hasLeftView ? 0 : self.textPadding.width;
650+
CGFloat rightPadding = hasRightView ? rightRect.size.width : self.textPadding.width * 2;
651+
652+
CGRect rect = CGRectMake(superRect.origin.x + leftPadding,
646653
[self adjustedYPositionForTextRect],
647-
superRect.size.width - (2.0 * self.textPadding.width),
654+
superRect.size.width - rightPadding,
648655
self.font.lineHeight);
649656
self.textRect = rect;
650657
return rect;
@@ -680,6 +687,7 @@ - (CGRect)clearButtonRectForBounds:(CGRect)bounds
680687
- (CGRect)rightViewRectForBounds:(CGRect)bounds
681688
{
682689
CGRect rightViewRect = [super rightViewRectForBounds:bounds];
690+
rightViewRect.origin.x = rightViewRect.origin.x - self.textPadding.width;
683691
rightViewRect.origin.y = CGRectGetMidY(_textRect) - (rightViewRect.size.height / 2.0f);
684692

685693
return rightViewRect;
@@ -688,6 +696,7 @@ - (CGRect)rightViewRectForBounds:(CGRect)bounds
688696
- (CGRect)leftViewRectForBounds:(CGRect)bounds
689697
{
690698
CGRect leftViewRect = [super leftViewRectForBounds:bounds];
699+
leftViewRect.origin.x = self.textPadding.width;
691700
leftViewRect.origin.y = CGRectGetMidY(_textRect) - (leftViewRect.size.height / 2.0f);
692701

693702
return leftViewRect;

MaterialTextFieldDemo/MaterialTextFieldDemo/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<outlet property="delegate" destination="BYZ-38-t0r" id="0u6-qT-cZ2"/>
7474
</connections>
7575
</textField>
76-
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="placeholder" adjustsFontSizeToFit="NO" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Z0L-Ka-jzP" customClass="MFTextField">
76+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="placeholder" adjustsFontSizeToFit="NO" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="Z0L-Ka-jzP" customClass="MFTextField">
7777
<rect key="frame" x="16" y="272.5" width="343" height="40"/>
7878
<color key="tintColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
7979
<fontDescription key="fontDescription" type="system" pointSize="20"/>

0 commit comments

Comments
 (0)