Skip to content

Commit

Permalink
add left Image
Browse files Browse the repository at this point in the history
  • Loading branch information
devikkim committed Jun 27, 2019
1 parent b422757 commit ea2bfeb
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 20 deletions.
49 changes: 36 additions & 13 deletions Example/ValidationTextField/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
<subviews>
<view contentMode="scaleToFill" placeholderIntrinsicWidth="375" placeholderIntrinsicHeight="667" translatesAutoresizingMaskIntoConstraints="NO" id="CQr-Ds-yFx">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="570"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Wbh-vn-Xuz">
<rect key="frame" x="87.5" y="567" width="200" height="50"/>
<rect key="frame" x="87.5" y="470" width="200" height="50"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<accessibility key="accessibilityConfiguration">
<accessibilityTraits key="traits" button="YES" causesPageTurn="YES"/>
Expand All @@ -42,60 +42,80 @@
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="36" translatesAutoresizingMaskIntoConstraints="NO" id="Lgb-wh-zvi">
<rect key="frame" x="30" y="20" width="315" height="497"/>
<rect key="frame" x="30" y="20" width="315" height="400"/>
<subviews>
<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">
<rect key="frame" x="0.0" y="0.0" width="315" height="97.5"/>
<rect key="frame" x="0.0" y="0.0" width="315" height="51"/>
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="It's good name!"/>
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="sorry, enter your name!"/>
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter your name"/>
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Name"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="YES"/>
</userDefinedRuntimeAttributes>
</textField>
<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">
<rect key="frame" x="0.0" y="133.5" width="315" height="97"/>
<rect key="frame" x="0.0" y="87" width="315" height="51.5"/>
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet" secureTextEntry="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter password"/>
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="Valid password"/>
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="Invalid password"/>
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Password"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="YES"/>
</userDefinedRuntimeAttributes>
</textField>
<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">
<rect key="frame" x="0.0" y="266.5" width="315" height="97.5"/>
<rect key="frame" x="0.0" y="174.5" width="315" height="51"/>
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet" secureTextEntry="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="It matched password!"/>
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Confirm password"/>
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="It is no matched password."/>
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Confirm password"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="YES"/>
</userDefinedRuntimeAttributes>
</textField>
<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">
<rect key="frame" x="0.0" y="400" width="315" height="97"/>
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<rect key="frame" x="0.0" y="261.5" width="315" height="51.5"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="completedTitleText" value="Valid Email"/>
<userDefinedRuntimeAttribute type="string" keyPath="errorMessage" value="Invalid Email!"/>
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Enter your email"/>
<userDefinedRuntimeAttribute type="string" keyPath="titleText" value="Email"/>
</userDefinedRuntimeAttributes>
</textField>
<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">
<rect key="frame" x="0.0" y="349" width="315" height="51"/>
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="25"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" keyboardType="alphabet"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="Search"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="isShowTitle" value="NO"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="isUseTitle" value="NO"/>
<userDefinedRuntimeAttribute type="image" keyPath="leftImage" value="search"/>
<userDefinedRuntimeAttribute type="color" keyPath="titleColor">
<color key="value" red="0.14117647058823529" green="0.51764705882352935" blue="0.96862745098039216" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="disabledColor">
<color key="value" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="placeholderColor">
<color key="value" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</textField>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="400" id="y3T-I2-s37"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand Down Expand Up @@ -141,4 +161,7 @@
<point key="canvasLocation" x="136.80000000000001" y="133.5832083958021"/>
</scene>
</scenes>
<resources>
<image name="search" width="128" height="128"/>
</resources>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "iconfinder_icon-111-search_314689.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ValidationTextField.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'ValidationTextField'
s.version = '1.0.2'
s.version = '1.0.3'
s.summary = 'This TextField for Validation ( complete or error )'

# This description is used to generate tags and improve search results.
Expand Down
62 changes: 56 additions & 6 deletions ValidationTextField/Classes/ValidationTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ open class ValidationTextField: UITextField {
update()
}
}
@IBInspectable
open var isUseTitle: Bool = true {
didSet {
update()
}
}

@IBInspectable
open var titleText: String = "TITLE" {
didSet {
update()

statusImageView.heightAnchor.constraint(equalToConstant: max(titleFont.lineHeight, titleLabel.intrinsicContentSize.height)).isActive = true
statusImageView.widthAnchor.constraint(equalToConstant: max(titleFont.lineHeight, titleLabel.intrinsicContentSize.height)).isActive = true

statusImageView.layoutIfNeeded()
}
}

Expand Down Expand Up @@ -104,6 +115,16 @@ open class ValidationTextField: UITextField {
}
}

@IBInspectable
open var leftImage: UIImage? {
didSet {
leftViewMode = .always
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = leftImage
leftView = imageView
}
}


// MARK: Properties

Expand Down Expand Up @@ -212,18 +233,18 @@ open class ValidationTextField: UITextField {

statusImageView.translatesAutoresizingMaskIntoConstraints = false
statusImageView.contentMode = .scaleAspectFill
statusImageView.heightAnchor.constraint(equalToConstant: titleFont.lineHeight).isActive = true
statusImageView.widthAnchor.constraint(equalToConstant: titleFont.lineHeight).isActive = true

containerView = UIStackView()
containerView.axis = .horizontal
containerView.spacing = 5

containerView.addArrangedSubview(titleLabel)
containerView.addArrangedSubview(statusImageView)
containerView.addArrangedSubview(UILabel())

containerView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
containerView.translatesAutoresizingMaskIntoConstraints = false

addSubview(containerView)
}

Expand Down Expand Up @@ -371,11 +392,23 @@ open class ValidationTextField: UITextField {
lineView.backgroundColor = titleColor
}

if isUseTitle {
containerView.isHidden = false
containerView.alpha = 1.0
} else {
containerView.isHidden = true
containerView.alpha = 0.0
}

titleLabel.text = titleText
titleLabel.textColor = titleColor
titleLabel.font = titleFont

updateTitleVisibility(true)

if !isEnabled {
lineView.backgroundColor = disabledColor
}
}

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

let padding: CGFloat = leftImage == nil ? 0 : 5

return CGRect(
x: superRect.origin.x,
x: superRect.origin.x + padding,
y: titleHeight,
width: superRect.size.width,
height: superRect.size.height - titleHeight - selectedLineHeight
)
}

override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
let superRect = super.editingRect(forBounds: bounds)
let titleHeight = self.titleHeight()

let padding: CGFloat = leftImage == nil ? 0 : 5

let rect = CGRect(
x: 0,
y: titleHeight(),
x: superRect.origin.x + padding,
y: titleHeight,
width: bounds.size.width,
height: bounds.size.height - titleHeight() - selectedLineHeight
height: bounds.size.height - titleHeight - selectedLineHeight
)
return rect
}

override open func leftViewRect(forBounds bounds: CGRect) -> CGRect {
let rect = CGRect (
x: 0,
y: (titleHeight() + (bounds.size.height - titleHeight() - selectedLineHeight) / 2) - 10 ,
width: 20,
height: 20
)
return rect
}
Expand Down

0 comments on commit ea2bfeb

Please sign in to comment.