Skip to content

Commit

Permalink
Add placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
Datt committed Nov 18, 2019
1 parent 8130bda commit 47c1ab5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 32 deletions.
Binary file not shown.
19 changes: 19 additions & 0 deletions DPOTPView/DPOTPView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<userDefinedRuntimeAttribute type="boolean" keyPath="isCircleTextField" value="YES"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="isCursorHidden" value="YES"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="dismissOnLastEntry" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="selectedBorderWidthTextField">
<real key="value" value="3"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="XZU-8o-pmY" customClass="DPOTPView" customModule="DPOTPView" customModuleProvider="target">
Expand All @@ -69,6 +72,9 @@
<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>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="XWD-DZ-vzi" customClass="DPOTPView" customModule="DPOTPView" customModuleProvider="target">
Expand Down Expand Up @@ -97,6 +103,13 @@
<color key="value" systemColor="systemTealColor" red="0.35294117650000001" green="0.7843137255" blue="0.98039215690000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="isDarkKeyboard" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="selectedBorderWidthTextField">
<real key="value" value="3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="placeholderTextColor">
<color key="value" systemColor="systemRedColor" red="1" green="0.23137254900000001" blue="0.18823529410000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="placeholder" value="✲✻✵✫✱✶"/>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="DtP-Ci-7qP" customClass="DPOTPView" customModule="DPOTPView" customModuleProvider="target">
Expand All @@ -121,6 +134,9 @@
<color key="value" systemColor="systemRedColor" red="1" green="0.23137254900000001" blue="0.18823529410000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="dismissOnLastEntry" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="selectedBorderWidthTextField">
<real key="value" value="4"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="tOx-qg-TIm" customClass="DPOTPView" customModule="DPOTPView" customModuleProvider="target">
Expand Down Expand Up @@ -149,6 +165,9 @@
<color key="value" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="isCursorHidden" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="selectedBorderWidthTextField">
<real key="value" value="2"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
</subviews>
Expand Down
92 changes: 60 additions & 32 deletions DPOTPView/DPOTPView/DPOTPView/DPOTPView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ protocol DPOTPViewDelegate {
/** Spaceing between textField in the DPOTPView */
@IBInspectable var spacing: CGFloat = 8

/** Circle textField */
@IBInspectable var isCircleTextField: Bool = false
/** Text color for the textField */
@IBInspectable var textColorTextField: UIColor = UIColor.black

/** Text font for the textField */
@IBInspectable var fontTextField: UIFont = UIFont.systemFont(ofSize: 20) { didSet { setNeedsDisplay() } }
@IBInspectable var fontTextField: UIFont = UIFont.systemFont(ofSize: 25)

/** Text color for the textField */
@IBInspectable var textColorTextField: UIColor = UIColor.black
/** Placeholder */
@IBInspectable var placeholder: String = ""

/** Placeholder text color for the textField */
@IBInspectable var placeholderTextColor: UIColor = UIColor.gray

/** Circle textField */
@IBInspectable var isCircleTextField: Bool = false

/** Allow only Bottom Line for the TextField */
@IBInspectable var isBottomLineTextField: Bool = false

/** Background color for the textField */
@IBInspectable var backGroundColorTextField: UIColor = UIColor.clear
Expand All @@ -45,12 +54,12 @@ protocol DPOTPViewDelegate {
/** Border width for the TextField */
@IBInspectable var borderWidthTextField: CGFloat = 0.0

/** Border width for the TextField */
@IBInspectable var selectedBorderWidthTextField: CGFloat = 0.0

/** Corner radius for the TextField */
@IBInspectable var cornerRadiusTextField: CGFloat = 0.0

/** Allow only Bottom Line for the TextField */
@IBInspectable var isBottomLineTextField: Bool = false

/** Tint/cursor color for the TextField */
@IBInspectable var tintColorTextField: UIColor = UIColor.systemBlue

Expand All @@ -77,6 +86,7 @@ protocol DPOTPViewDelegate {
var editingTextEdgeInsets : UIEdgeInsets?

var dpOTPViewDelegate : DPOTPViewDelegate?
var keyboardType:UIKeyboardType = UIKeyboardType.asciiCapableNumberPad

var text : String? {
get {
Expand All @@ -100,25 +110,27 @@ protocol DPOTPViewDelegate {
#if !TARGET_INTERFACE_BUILDER
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialization()
}

/** Override common init, for manual allocation */
override init(frame: CGRect) {
super.init(frame: frame)
initialization()
}

override func layoutSubviews() {
super.layoutSubviews()
self.initialization()
}
#endif

func initialization() {

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
initialization()
}

// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
func initialization() {
if arrTextFields.count != 0 { return }
// Drawing code

let sizeTextField = (self.bounds.width/CGFloat(count)) - (spacing)

for i in 1 ... count {
Expand Down Expand Up @@ -154,29 +166,30 @@ protocol DPOTPViewDelegate {
}
textField.textColor = textColorTextField
textField.textAlignment = .center
textField.keyboardType = .asciiCapableNumberPad
textField.keyboardType = keyboardType
if #available(iOS 12.0, *) {
textField.textContentType = .oneTimeCode
}

if placeholder.count > i - 1 {
textField.attributedPlaceholder = NSAttributedString(string: placeholder[i - 1],
attributes: [NSAttributedString.Key.foregroundColor: placeholderTextColor])
}

textField.frame = CGRect(x:(CGFloat(i-1) * sizeTextField) + (CGFloat(i) * spacing/2) + (CGFloat(i-1) * spacing/2) , y: (self.bounds.height - sizeTextField)/2 , width: sizeTextField, height: sizeTextField)

arrTextFields.append(textField)
self.addSubview(textField)
}
super.draw(rect)
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
OperationQueue.main.addOperation({
if #available(iOS 13.0, *) {
UIMenuController.shared.hideMenu()
} else {
UIMenuController.shared.setMenuVisible(false, animated: false)
}
})
return super.canPerformAction(action, withSender: sender)
}
// // Only override draw() if you perform custom drawing.
// // An empty implementation adversely affects performance during animation.
// override func draw(_ rect: CGRect) {
//
// super.draw(rect)
// }


override func becomeFirstResponder() -> Bool {
if isCursorHidden {
Expand Down Expand Up @@ -268,6 +281,19 @@ class OTPBackTextField: UITextField {
OTPBackDelegate?.textFieldDidDelete(self)
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
// if action == #selector(UIResponderStandardEditActions.copy(_:)) ||
// action == #selector(UIResponderStandardEditActions.cut(_:)) ||
// action == #selector(UIResponderStandardEditActions.select(_:)) ||
// action == #selector(UIResponderStandardEditActions.selectAll(_:)) ||
// action == #selector(UIResponderStandardEditActions.delete(_:)) {
//
// return false
// }
// return super.canPerformAction(action, withSender: sender)
return false
}

override func becomeFirstResponder() -> Bool {
addSelectedBorderColor()
return super.becomeFirstResponder()
Expand All @@ -281,29 +307,31 @@ class OTPBackTextField: UITextField {
fileprivate func addSelectedBorderColor() {
if let selectedBorderColor = dpOTPView.selectedBorderColorTextField {
if dpOTPView.isBottomLineTextField {
addBottomLine(selectedBorderColor)
addBottomLine(selectedBorderColor, width: dpOTPView.selectedBorderWidthTextField)
} else {
layer.borderColor = selectedBorderColor.cgColor
layer.borderWidth = dpOTPView.selectedBorderWidthTextField
}
}
}

fileprivate func addUnselectedBorderColor() {
if let unselectedBorderColor = dpOTPView.borderColorTextField {
if dpOTPView.isBottomLineTextField {
addBottomLine(unselectedBorderColor)
addBottomLine(unselectedBorderColor, width: dpOTPView.borderWidthTextField)
} else {
layer.borderColor = unselectedBorderColor.cgColor
layer.borderWidth = dpOTPView.borderWidthTextField
}
}
}

fileprivate func addBottomLine(_ color : UIColor) {
fileprivate func addBottomLine(_ color : UIColor , width : CGFloat) {
let border = CALayer()
border.name = "bottomBorderLayer"
removePreviouslyAddedLayer(name: border.name ?? "")
border.backgroundColor = color.cgColor
border.frame = CGRect(x: 0, y: self.frame.width - dpOTPView.borderWidthTextField,width : self.frame.width ,height: dpOTPView.borderWidthTextField)
border.frame = CGRect(x: 0, y: self.frame.width - width ,width : self.frame.width ,height: width)
self.layer.addSublayer(border)
}

Expand Down

0 comments on commit 47c1ab5

Please sign in to comment.