Skip to content

Commit ea2bfeb

Browse files
committed
add left Image
1 parent b422757 commit ea2bfeb

File tree

5 files changed

+114
-20
lines changed

5 files changed

+114
-20
lines changed

Example/ValidationTextField/Base.lproj/Main.storyboard

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
2323
<subviews>
2424
<view contentMode="scaleToFill" placeholderIntrinsicWidth="375" placeholderIntrinsicHeight="667" translatesAutoresizingMaskIntoConstraints="NO" id="CQr-Ds-yFx">
25-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
25+
<rect key="frame" x="0.0" y="0.0" width="375" height="570"/>
2626
<subviews>
2727
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Wbh-vn-Xuz">
28-
<rect key="frame" x="87.5" y="567" width="200" height="50"/>
28+
<rect key="frame" x="87.5" y="470" width="200" height="50"/>
2929
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
3030
<accessibility key="accessibilityConfiguration">
3131
<accessibilityTraits key="traits" button="YES" causesPageTurn="YES"/>
@@ -42,60 +42,80 @@
4242
</connections>
4343
</button>
4444
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="36" translatesAutoresizingMaskIntoConstraints="NO" id="Lgb-wh-zvi">
45-
<rect key="frame" x="30" y="20" width="315" height="497"/>
45+
<rect key="frame" x="30" y="20" width="315" height="400"/>
4646
<subviews>
4747
<textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="WjA-dA-utA" customClass="ValidationTextField" customModule="ValidationTextField">
48-
<rect key="frame" x="0.0" y="0.0" width="315" height="97.5"/>
48+
<rect key="frame" x="0.0" y="0.0" width="315" height="51"/>
4949
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
50-
<fontDescription key="fontDescription" type="system" pointSize="14"/>
50+
<fontDescription key="fontDescription" type="system" pointSize="15"/>
5151
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet"/>
5252
<userDefinedRuntimeAttributes>
53-
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="It's good name!"/>
5453
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="sorry, enter your name!"/>
5554
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter your name"/>
5655
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Name"/>
5756
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="YES"/>
5857
</userDefinedRuntimeAttributes>
5958
</textField>
6059
<textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="CkR-sk-DYQ" customClass="ValidationTextField" customModule="ValidationTextField">
61-
<rect key="frame" x="0.0" y="133.5" width="315" height="97"/>
60+
<rect key="frame" x="0.0" y="87" width="315" height="51.5"/>
6261
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
6362
<fontDescription key="fontDescription" type="system" pointSize="14"/>
6463
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet" secureTextEntry="YES"/>
6564
<userDefinedRuntimeAttributes>
6665
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter password"/>
67-
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="Valid password"/>
6866
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="Invalid password"/>
6967
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Password"/>
7068
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="YES"/>
7169
</userDefinedRuntimeAttributes>
7270
</textField>
7371
<textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="rw0-nI-IKX" customClass="ValidationTextField" customModule="ValidationTextField">
74-
<rect key="frame" x="0.0" y="266.5" width="315" height="97.5"/>
72+
<rect key="frame" x="0.0" y="174.5" width="315" height="51"/>
7573
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
7674
<fontDescription key="fontDescription" type="system" pointSize="14"/>
7775
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet" secureTextEntry="YES"/>
7876
<userDefinedRuntimeAttributes>
79-
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="It matched password!"/>
8077
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Confirm password"/>
8178
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="It is no matched password."/>
8279
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Confirm password"/>
8380
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="YES"/>
8481
</userDefinedRuntimeAttributes>
8582
</textField>
8683
<textField opaque="NO" contentMode="scaleToFill" horizontalCompressionResistancePriority="751" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="pJO-sQ-ntS" customClass="ValidationTextField" customModule="ValidationTextField">
87-
<rect key="frame" x="0.0" y="400" width="315" height="97"/>
88-
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
84+
<rect key="frame" x="0.0" y="261.5" width="315" height="51.5"/>
85+
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
8986
<fontDescription key="fontDescription" type="system" pointSize="14"/>
9087
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet"/>
9188
<userDefinedRuntimeAttributes>
92-
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="Valid Email"/>
9389
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="Invalid Email!"/>
9490
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter your email"/>
9591
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Email"/>
9692
</userDefinedRuntimeAttributes>
9793
</textField>
94+
<textField opaque="NO" contentMode="scaleToFill" horizontalCompressionResistancePriority="751" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="A7W-vO-I0g" customClass="ValidationTextField" customModule="ValidationTextField">
95+
<rect key="frame" x="0.0" y="349" width="315" height="51"/>
96+
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
97+
<fontDescription key="fontDescription" type="system" pointSize="25"/>
98+
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet"/>
99+
<userDefinedRuntimeAttributes>
100+
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Search"/>
101+
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="NO"/>
102+
<userDefinedRuntimeAttribute type="boolean" keyPath="isUseTitle" value="NO"/>
103+
<userDefinedRuntimeAttribute type="image" keyPath="leftImage" value="search"/>
104+
<userDefinedRuntimeAttribute type="color" keyPath="titleColor">
105+
<color key="value" red="0.14117647058823529" green="0.51764705882352935" blue="0.96862745098039216" alpha="1" colorSpace="calibratedRGB"/>
106+
</userDefinedRuntimeAttribute>
107+
<userDefinedRuntimeAttribute type="color" keyPath="disabledColor">
108+
<color key="value" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
109+
</userDefinedRuntimeAttribute>
110+
<userDefinedRuntimeAttribute type="color" keyPath="placeholderColor">
111+
<color key="value" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
112+
</userDefinedRuntimeAttribute>
113+
</userDefinedRuntimeAttributes>
114+
</textField>
98115
</subviews>
116+
<constraints>
117+
<constraint firstAttribute="height" constant="400" id="y3T-I2-s37"/>
118+
</constraints>
99119
</stackView>
100120
</subviews>
101121
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
@@ -141,4 +161,7 @@
141161
<point key="canvasLocation" x="136.80000000000001" y="133.5832083958021"/>
142162
</scene>
143163
</scenes>
164+
<resources>
165+
<image name="search" width="128" height="128"/>
166+
</resources>
144167
</document>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"filename" : "iconfinder_icon-111-search_314689.png",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
2.79 KB
Loading

ValidationTextField.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'ValidationTextField'
11-
s.version = '1.0.2'
11+
s.version = '1.0.3'
1212
s.summary = 'This TextField for Validation ( complete or error )'
1313

1414
# This description is used to generate tags and improve search results.

ValidationTextField/Classes/ValidationTextField.swift

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,22 @@ open class ValidationTextField: UITextField {
3737
update()
3838
}
3939
}
40+
@IBInspectable
41+
open var isUseTitle: Bool = true {
42+
didSet {
43+
update()
44+
}
45+
}
4046

4147
@IBInspectable
4248
open var titleText: String = "TITLE" {
4349
didSet {
4450
update()
51+
52+
statusImageView.heightAnchor.constraint(equalToConstant: max(titleFont.lineHeight, titleLabel.intrinsicContentSize.height)).isActive = true
53+
statusImageView.widthAnchor.constraint(equalToConstant: max(titleFont.lineHeight, titleLabel.intrinsicContentSize.height)).isActive = true
54+
55+
statusImageView.layoutIfNeeded()
4556
}
4657
}
4758

@@ -104,6 +115,16 @@ open class ValidationTextField: UITextField {
104115
}
105116
}
106117

118+
@IBInspectable
119+
open var leftImage: UIImage? {
120+
didSet {
121+
leftViewMode = .always
122+
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
123+
imageView.image = leftImage
124+
leftView = imageView
125+
}
126+
}
127+
107128

108129
// MARK: Properties
109130

@@ -212,18 +233,18 @@ open class ValidationTextField: UITextField {
212233

213234
statusImageView.translatesAutoresizingMaskIntoConstraints = false
214235
statusImageView.contentMode = .scaleAspectFill
215-
statusImageView.heightAnchor.constraint(equalToConstant: titleFont.lineHeight).isActive = true
216-
statusImageView.widthAnchor.constraint(equalToConstant: titleFont.lineHeight).isActive = true
217236

218237
containerView = UIStackView()
219238
containerView.axis = .horizontal
220239
containerView.spacing = 5
221240

222241
containerView.addArrangedSubview(titleLabel)
223242
containerView.addArrangedSubview(statusImageView)
243+
containerView.addArrangedSubview(UILabel())
224244

225245
containerView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
226246
containerView.translatesAutoresizingMaskIntoConstraints = false
247+
227248
addSubview(containerView)
228249
}
229250

@@ -371,11 +392,23 @@ open class ValidationTextField: UITextField {
371392
lineView.backgroundColor = titleColor
372393
}
373394

395+
if isUseTitle {
396+
containerView.isHidden = false
397+
containerView.alpha = 1.0
398+
} else {
399+
containerView.isHidden = true
400+
containerView.alpha = 0.0
401+
}
402+
374403
titleLabel.text = titleText
375404
titleLabel.textColor = titleColor
376405
titleLabel.font = titleFont
377406

378407
updateTitleVisibility(true)
408+
409+
if !isEnabled {
410+
lineView.backgroundColor = disabledColor
411+
}
379412
}
380413

381414
private func updatePlaceholder() {
@@ -421,20 +454,37 @@ open class ValidationTextField: UITextField {
421454
let superRect = super.editingRect(forBounds: bounds)
422455
let titleHeight = self.titleHeight()
423456

457+
let padding: CGFloat = leftImage == nil ? 0 : 5
458+
424459
return CGRect(
425-
x: superRect.origin.x,
460+
x: superRect.origin.x + padding,
426461
y: titleHeight,
427462
width: superRect.size.width,
428463
height: superRect.size.height - titleHeight - selectedLineHeight
429464
)
430465
}
431466

432467
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
468+
let superRect = super.editingRect(forBounds: bounds)
469+
let titleHeight = self.titleHeight()
470+
471+
let padding: CGFloat = leftImage == nil ? 0 : 5
472+
433473
let rect = CGRect(
434-
x: 0,
435-
y: titleHeight(),
474+
x: superRect.origin.x + padding,
475+
y: titleHeight,
436476
width: bounds.size.width,
437-
height: bounds.size.height - titleHeight() - selectedLineHeight
477+
height: bounds.size.height - titleHeight - selectedLineHeight
478+
)
479+
return rect
480+
}
481+
482+
override open func leftViewRect(forBounds bounds: CGRect) -> CGRect {
483+
let rect = CGRect (
484+
x: 0,
485+
y: (titleHeight() + (bounds.size.height - titleHeight() - selectedLineHeight) / 2) - 10 ,
486+
width: 20,
487+
height: 20
438488
)
439489
return rect
440490
}

0 commit comments

Comments
 (0)