|
| 1 | +// |
| 2 | +// ABCPinTextFieldExample.swift |
| 3 | +// ABCPinTextField_demo |
| 4 | +// |
| 5 | +// Created by AKACoder on 2017/12/25. |
| 6 | +// Copyright © 2017年 personal. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import Neon |
| 11 | +//import ABCPinTextFieldInput |
| 12 | + |
| 13 | +class ABCPinTextFieldExample { |
| 14 | + //get pin input with default configure |
| 15 | + var pinInputWithDash: ABCPinTextField! = nil |
| 16 | + |
| 17 | + //get pin input with some configure |
| 18 | + var pinInputWithBottomBorder: ABCPinTextField! = nil |
| 19 | + var passwordPinInputWithDash: ABCPinTextField! = nil |
| 20 | + var passwordPinInputWithBottomBorder: ABCPinTextField! = nil |
| 21 | + |
| 22 | + //custom draw pin input |
| 23 | + var customPinInput: ABCPinTextField! = nil |
| 24 | + |
| 25 | + init(view: UIView) { |
| 26 | + CreatePinInput() |
| 27 | + SetDelegate() |
| 28 | + LayoutAllInput(on: view, inputs: |
| 29 | + [ |
| 30 | + pinInputWithDash, |
| 31 | + pinInputWithBottomBorder, |
| 32 | + passwordPinInputWithDash, |
| 33 | + passwordPinInputWithBottomBorder, |
| 34 | + customPinInput |
| 35 | + ] |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +extension ABCPinTextFieldExample { |
| 41 | + func LayoutAllInput(on view: UIView, inputs: [ABCPinTextField]) { |
| 42 | + let inputWidth = UIScreen.main.bounds.width - 20 |
| 43 | + let inputHeight: CGFloat = 60 |
| 44 | + let inputPadding: CGFloat = 20 |
| 45 | + |
| 46 | + var prevInput: ABCPinTextField? = nil |
| 47 | + for (idx, pinInput) in inputs.enumerated() { |
| 48 | + view.addSubview(pinInput) |
| 49 | + if(0 == idx) { |
| 50 | + pinInput.anchorToEdge(Edge.top, padding: 90, |
| 51 | + width: inputWidth, |
| 52 | + height: inputHeight) |
| 53 | + } else { |
| 54 | + pinInput.align(Align.underCentered, |
| 55 | + relativeTo: prevInput!, |
| 56 | + padding: inputPadding, |
| 57 | + width: inputWidth, |
| 58 | + height: inputHeight) |
| 59 | + } |
| 60 | + |
| 61 | + pinInput.Layout() |
| 62 | + prevInput = pinInput |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension ABCPinTextFieldExample: ABCPinTextFieldDelegate { |
| 68 | + func Enabled() { |
| 69 | + print("Enabled") |
| 70 | + } |
| 71 | + |
| 72 | + func Disabled() { |
| 73 | + print("Disabled") |
| 74 | + } |
| 75 | + func FinishedResign() { |
| 76 | + print("FinishedResign") |
| 77 | + } |
| 78 | + func UnfinishedResign() { |
| 79 | + print("UnfinishedResign") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension ABCPinTextFieldExample { |
| 84 | + func SetDelegate() { |
| 85 | + pinInputWithDash.Delegate = self |
| 86 | + |
| 87 | + pinInputWithBottomBorder.Delegate = self |
| 88 | + passwordPinInputWithDash.Delegate = self |
| 89 | + passwordPinInputWithBottomBorder.Delegate = self |
| 90 | + |
| 91 | + customPinInput.Delegate = self |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +extension ABCPinTextFieldExample { |
| 96 | + func CreatePinInput() { |
| 97 | + //get pin input with default configure |
| 98 | + pinInputWithDash = GetPinInputWithDash() |
| 99 | + |
| 100 | + //get pin input with some configure |
| 101 | + pinInputWithBottomBorder = GetPinInputWithBorder(pinCount: 4) |
| 102 | + passwordPinInputWithDash = GetPasswordPinInputWithDash(pinCount: 4) |
| 103 | + passwordPinInputWithBottomBorder = GetPasswordPinInputWithBorder(pinCount: 8) |
| 104 | + |
| 105 | + //custom draw pin input |
| 106 | + customPinInput = GetCustomPinInput() |
| 107 | + } |
| 108 | + |
| 109 | + func GetPinInputWithDash() -> ABCPinTextField { |
| 110 | + let config = ABCPinTextFieldConfig() |
| 111 | + return ABCPinTextField(with: config) |
| 112 | + } |
| 113 | + |
| 114 | + func GetPinInputWithBorder(pinCount: Int) -> ABCPinTextField { |
| 115 | + let config = ABCPinTextFieldConfig() |
| 116 | + config.Mode = ABCPinMode.PlainTextWithBottomBorder(borderColor: UIColor.blue, |
| 117 | + borderWidth: 40, borderHeight: 2) |
| 118 | + |
| 119 | + config.PinCount = pinCount |
| 120 | + return ABCPinTextField(with: config) |
| 121 | + } |
| 122 | + |
| 123 | + func GetPasswordPinInputWithDash(pinCount: Int) -> ABCPinTextField { |
| 124 | + let config = ABCPinTextFieldConfig() |
| 125 | + config.Mode = ABCPinMode.PasswordTextWithDash(dashColor: UIColor.red, |
| 126 | + dashWidth: 30, |
| 127 | + dashHeight: 2) |
| 128 | + |
| 129 | + config.PinCount = pinCount |
| 130 | + return ABCPinTextField(with: config) |
| 131 | + } |
| 132 | + |
| 133 | + func GetPasswordPinInputWithBorder(pinCount: Int) -> ABCPinTextField { |
| 134 | + let config = ABCPinTextFieldConfig() |
| 135 | + config.Mode = ABCPinMode.PasswordTextWithBottomBorder(borderColor: UIColor.gray, |
| 136 | + borderWidth: 30, borderHeight: 2) |
| 137 | + |
| 138 | + config.PinCount = pinCount |
| 139 | + return ABCPinTextField(with: config) |
| 140 | + } |
| 141 | + |
| 142 | + func GetCustomPinInput() -> ABCPinTextField { |
| 143 | + let config = ABCPinTextFieldConfig() |
| 144 | + config.Mode = ABCPinMode.CustomMode(customDraw: { (input, idx) in |
| 145 | + //set corner radius |
| 146 | + input.layer.masksToBounds = true |
| 147 | + input.layer.cornerRadius = 4 |
| 148 | + //set background |
| 149 | + input.backgroundColor = UIColor.lightGray |
| 150 | + }) |
| 151 | + |
| 152 | + return ABCPinTextField(with: config) |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments