Skip to content

Commit

Permalink
Add Shadow Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Datt committed Nov 21, 2019
1 parent a74dd8a commit 275165c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
Binary file not shown.
23 changes: 19 additions & 4 deletions DPOTPView/DPOTPView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,33 @@
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="borderColorTextField">
<color key="value" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="value" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidthTextField">
<real key="value" value="1.5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadiusTextField">
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="dismissOnLastEntry" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="selectedBorderWidthTextField">
<real key="value" value="1.5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowRadiusTextField">
<real key="value" value="5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowOpacityTextField">
<real key="value" value="0.5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="shadowColorTextField">
<color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="size" keyPath="shadowOffsetSizeTextField">
<size key="value" width="0.0" height="3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadiusTextField">
<real key="value" value="15"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="backGroundColorTextField">
<color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="XWD-DZ-vzi" customClass="DPOTPView" customModule="DPOTPView" customModuleProvider="target">
Expand Down
58 changes: 43 additions & 15 deletions DPOTPView/DPOTPView/DPOTPView/DPOTPView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,26 @@ protocol DPOTPViewDelegate {
/** Tint/cursor color for the TextField */
@IBInspectable var tintColorTextField: UIColor = UIColor.systemBlue

/** Shadow Radius for the TextField */
@IBInspectable var shadowRadiusTextField: CGFloat = 0.0

/** Shadow Opacity for the TextField */
@IBInspectable var shadowOpacityTextField: Float = 0.0

/** Shadow Offset Size for the TextField */
@IBInspectable var shadowOffsetSizeTextField: CGSize = .zero

/** Shadow color for the TextField */
@IBInspectable var shadowColorTextField: UIColor?

/** Dismiss keyboard with enter last character*/
@IBInspectable var dismissOnLastEntry: Bool = false

/** Secure Text Entry*/
@IBInspectable var isSecureTextEntry: Bool = false

/** Hide cursor*/
@IBInspectable var isCursorHidden: Bool = false {
didSet {
if isCursorHidden {
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
self.addGestureRecognizer(tap)
}
}
}
@IBInspectable var isCursorHidden: Bool = false

/** Dark keyboard*/
@IBInspectable var isDarkKeyboard: Bool = false
Expand Down Expand Up @@ -158,12 +163,17 @@ protocol DPOTPViewDelegate {
textField.layer.borderWidth = borderWidthTextField
if isCircleTextField {
textField.layer.cornerRadius = sizeTextField / 2
textField.layer.masksToBounds = true
} else {
textField.layer.cornerRadius = cornerRadiusTextField
textField.layer.masksToBounds = cornerRadiusTextField > 0.0
}
}
textField.layer.shadowRadius = shadowRadiusTextField
if let shadowColorTextField = shadowColorTextField {
textField.layer.shadowColor = shadowColorTextField.cgColor
}
textField.layer.shadowOpacity = shadowOpacityTextField
textField.layer.shadowOffset = shadowOffsetSizeTextField

textField.textColor = textColorTextField
textField.textAlignment = .center
textField.keyboardType = keyboardType
Expand All @@ -180,6 +190,13 @@ protocol DPOTPViewDelegate {

arrTextFields.append(textField)
self.addSubview(textField)
if isCursorHidden {
let tapView = UIView(frame: self.bounds)
tapView.backgroundColor = .clear
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
tapView.addGestureRecognizer(tap)
self.addSubview(tapView)
}
}
}

Expand All @@ -197,8 +214,9 @@ protocol DPOTPViewDelegate {
if arrTextFields[i].text?.count == 0 {
_ = arrTextFields[i].becomeFirstResponder()
break
} else if (arrTextFields.count - 1) == i{
} else if (arrTextFields.count - 1) == i {
_ = arrTextFields[i].becomeFirstResponder()
break
}
}
} else {
Expand Down Expand Up @@ -234,10 +252,6 @@ protocol DPOTPViewDelegate {
extension DPOTPView : UITextFieldDelegate , OTPBackTextFieldDelegate {

func textFieldDidBeginEditing(_ textField: UITextField) {
if isCursorHidden {
(textField as? OTPBackTextField)?.addUnselectedBorderColor()
_ = self.becomeFirstResponder()
}
dpOTPViewDelegate?.dpOTPViewChangePositionAt(textField.tag/1000 - 1)
}

Expand Down Expand Up @@ -312,6 +326,13 @@ class OTPBackTextField: UITextField {
layer.borderColor = selectedBorderColor.cgColor
layer.borderWidth = dpOTPView.selectedBorderWidthTextField
}
} else {
if dpOTPView.isBottomLineTextField {
removePreviouslyAddedLayer(name: "bottomBorderLayer")
} else {
layer.borderColor = nil
layer.borderWidth = 0
}
}
}

Expand All @@ -323,6 +344,13 @@ class OTPBackTextField: UITextField {
layer.borderColor = unselectedBorderColor.cgColor
layer.borderWidth = dpOTPView.borderWidthTextField
}
} else {
if dpOTPView.isBottomLineTextField {
removePreviouslyAddedLayer(name: "bottomBorderLayer")
} else {
layer.borderColor = nil
layer.borderWidth = 0
}
}
}

Expand Down

0 comments on commit 275165c

Please sign in to comment.