Skip to content

Commit 275165c

Browse files
author
Datt
committed
Add Shadow Support
1 parent a74dd8a commit 275165c

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

DPOTPView/DPOTPView/Base.lproj/Main.storyboard

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,33 @@
6363
</constraints>
6464
<userDefinedRuntimeAttributes>
6565
<userDefinedRuntimeAttribute type="color" keyPath="borderColorTextField">
66-
<color key="value" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
66+
<color key="value" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
6767
</userDefinedRuntimeAttribute>
6868
<userDefinedRuntimeAttribute type="number" keyPath="borderWidthTextField">
6969
<real key="value" value="1.5"/>
7070
</userDefinedRuntimeAttribute>
71-
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadiusTextField">
72-
<real key="value" value="10"/>
73-
</userDefinedRuntimeAttribute>
7471
<userDefinedRuntimeAttribute type="boolean" keyPath="dismissOnLastEntry" value="YES"/>
7572
<userDefinedRuntimeAttribute type="number" keyPath="selectedBorderWidthTextField">
7673
<real key="value" value="1.5"/>
7774
</userDefinedRuntimeAttribute>
75+
<userDefinedRuntimeAttribute type="number" keyPath="shadowRadiusTextField">
76+
<real key="value" value="5"/>
77+
</userDefinedRuntimeAttribute>
78+
<userDefinedRuntimeAttribute type="number" keyPath="shadowOpacityTextField">
79+
<real key="value" value="0.5"/>
80+
</userDefinedRuntimeAttribute>
81+
<userDefinedRuntimeAttribute type="color" keyPath="shadowColorTextField">
82+
<color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
83+
</userDefinedRuntimeAttribute>
84+
<userDefinedRuntimeAttribute type="size" keyPath="shadowOffsetSizeTextField">
85+
<size key="value" width="0.0" height="3"/>
86+
</userDefinedRuntimeAttribute>
87+
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadiusTextField">
88+
<real key="value" value="15"/>
89+
</userDefinedRuntimeAttribute>
90+
<userDefinedRuntimeAttribute type="color" keyPath="backGroundColorTextField">
91+
<color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
92+
</userDefinedRuntimeAttribute>
7893
</userDefinedRuntimeAttributes>
7994
</view>
8095
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="XWD-DZ-vzi" customClass="DPOTPView" customModule="DPOTPView" customModuleProvider="target">

DPOTPView/DPOTPView/DPOTPView/DPOTPView.swift

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,26 @@ protocol DPOTPViewDelegate {
6363
/** Tint/cursor color for the TextField */
6464
@IBInspectable var tintColorTextField: UIColor = UIColor.systemBlue
6565

66+
/** Shadow Radius for the TextField */
67+
@IBInspectable var shadowRadiusTextField: CGFloat = 0.0
68+
69+
/** Shadow Opacity for the TextField */
70+
@IBInspectable var shadowOpacityTextField: Float = 0.0
71+
72+
/** Shadow Offset Size for the TextField */
73+
@IBInspectable var shadowOffsetSizeTextField: CGSize = .zero
74+
75+
/** Shadow color for the TextField */
76+
@IBInspectable var shadowColorTextField: UIColor?
77+
6678
/** Dismiss keyboard with enter last character*/
6779
@IBInspectable var dismissOnLastEntry: Bool = false
6880

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

7284
/** Hide cursor*/
73-
@IBInspectable var isCursorHidden: Bool = false {
74-
didSet {
75-
if isCursorHidden {
76-
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
77-
self.addGestureRecognizer(tap)
78-
}
79-
}
80-
}
85+
@IBInspectable var isCursorHidden: Bool = false
8186

8287
/** Dark keyboard*/
8388
@IBInspectable var isDarkKeyboard: Bool = false
@@ -158,12 +163,17 @@ protocol DPOTPViewDelegate {
158163
textField.layer.borderWidth = borderWidthTextField
159164
if isCircleTextField {
160165
textField.layer.cornerRadius = sizeTextField / 2
161-
textField.layer.masksToBounds = true
162166
} else {
163167
textField.layer.cornerRadius = cornerRadiusTextField
164-
textField.layer.masksToBounds = cornerRadiusTextField > 0.0
165168
}
166169
}
170+
textField.layer.shadowRadius = shadowRadiusTextField
171+
if let shadowColorTextField = shadowColorTextField {
172+
textField.layer.shadowColor = shadowColorTextField.cgColor
173+
}
174+
textField.layer.shadowOpacity = shadowOpacityTextField
175+
textField.layer.shadowOffset = shadowOffsetSizeTextField
176+
167177
textField.textColor = textColorTextField
168178
textField.textAlignment = .center
169179
textField.keyboardType = keyboardType
@@ -180,6 +190,13 @@ protocol DPOTPViewDelegate {
180190

181191
arrTextFields.append(textField)
182192
self.addSubview(textField)
193+
if isCursorHidden {
194+
let tapView = UIView(frame: self.bounds)
195+
tapView.backgroundColor = .clear
196+
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
197+
tapView.addGestureRecognizer(tap)
198+
self.addSubview(tapView)
199+
}
183200
}
184201
}
185202

@@ -197,8 +214,9 @@ protocol DPOTPViewDelegate {
197214
if arrTextFields[i].text?.count == 0 {
198215
_ = arrTextFields[i].becomeFirstResponder()
199216
break
200-
} else if (arrTextFields.count - 1) == i{
217+
} else if (arrTextFields.count - 1) == i {
201218
_ = arrTextFields[i].becomeFirstResponder()
219+
break
202220
}
203221
}
204222
} else {
@@ -234,10 +252,6 @@ protocol DPOTPViewDelegate {
234252
extension DPOTPView : UITextFieldDelegate , OTPBackTextFieldDelegate {
235253

236254
func textFieldDidBeginEditing(_ textField: UITextField) {
237-
if isCursorHidden {
238-
(textField as? OTPBackTextField)?.addUnselectedBorderColor()
239-
_ = self.becomeFirstResponder()
240-
}
241255
dpOTPViewDelegate?.dpOTPViewChangePositionAt(textField.tag/1000 - 1)
242256
}
243257

@@ -312,6 +326,13 @@ class OTPBackTextField: UITextField {
312326
layer.borderColor = selectedBorderColor.cgColor
313327
layer.borderWidth = dpOTPView.selectedBorderWidthTextField
314328
}
329+
} else {
330+
if dpOTPView.isBottomLineTextField {
331+
removePreviouslyAddedLayer(name: "bottomBorderLayer")
332+
} else {
333+
layer.borderColor = nil
334+
layer.borderWidth = 0
335+
}
315336
}
316337
}
317338

@@ -323,6 +344,13 @@ class OTPBackTextField: UITextField {
323344
layer.borderColor = unselectedBorderColor.cgColor
324345
layer.borderWidth = dpOTPView.borderWidthTextField
325346
}
347+
} else {
348+
if dpOTPView.isBottomLineTextField {
349+
removePreviouslyAddedLayer(name: "bottomBorderLayer")
350+
} else {
351+
layer.borderColor = nil
352+
layer.borderWidth = 0
353+
}
326354
}
327355
}
328356

0 commit comments

Comments
 (0)