Skip to content

Commit 701f5ee

Browse files
committed
init version
1 parent 323f11e commit 701f5ee

8 files changed

+872
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.0

ABCPinTextFieldInput.podspec

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Pod::Spec.new do |s|
2+
s.name = "ABCPinTextFieldInput"
3+
s.version = "0.1"
4+
s.summary = "A simple pin code input control / 一个简单的pin码输入控件"
5+
s.homepage = "https://github.com/AKACoder/ABCPinTextFieldInput/"
6+
s.license = { :type => "MIT" }
7+
s.author = { "AKACoder" => "akacoder@outlook.com" }
8+
s.platform = :ios, "10.0"
9+
s.source = { :git => "https://github.com/AKACoder/ABCPinTextFieldInput.git", :tag => "0.1" }
10+
s.source_files = "SourceCode/**/*.{swift}"
11+
s.requires_arc = true
12+
s.dependency "AsyncSwift", "2.0.4"
13+
s.dependency "Neon", "0.4.0"
14+
end

Example/ABCPinTextFieldExample.swift

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# ABCPinTextFieldInput
2+
一个简单的PIN码输入控件。
3+
4+
### 关于'ABC'
5+
- '**ABC**' 是 '**available but chaos**' 的缩写,用以说明我的水平不是很好。
6+
7+
### 依赖
8+
**[duemunk/Async](https://github.com/duemunk/Async)**
9+
如果你在使用
10+
Grand Central Dispatch
11+
([GCD](https://developer.apple.com/library/prerelease/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html))
12+
强烈建议你尝试一下Async!
13+
14+
**[mamaral/Neon](https://github.com/mamaral/Neon)**
15+
因为我太过笨拙,用不明白AutoLayout,所以使用了这个异常优秀的布局库。
16+
17+
### 安装
18+
cocoapods:
19+
> pod 'ABCPinTextFieldInput'
20+
21+
或者直接吧SourceCode下的源码拖到工程里
22+
23+
### 使用说明
24+
**ABCPinTextFieldInput 的 config**:
25+
```swift
26+
27+
//用户绘制输入框的界面的callback类型,第一个参数是input,第二个参数是给出的input的位置,第一个input是0,以此类推。
28+
public typealias ABCPinTextFieldInputCustomDraw = (UITextField, Int) -> Void
29+
30+
//输入框的模式
31+
public enum ABCPinMode {
32+
//明文文本,未输入时,使用小横线显示占位。参数为:横线颜色、横线宽度、横线高度
33+
case PlainTextWithDash(dashColor: UIColor, dashWidth: CGFloat, dashHeight: CGFloat)
34+
35+
//密文文本(输后显示小圆点),未输入时,使用小横线显示占位。参数为:横线颜色、横线宽度、横线高度
36+
case PasswordTextWithDash(dashColor: UIColor, dashWidth: CGFloat, dashHeight: CGFloat)
37+
38+
//明文文本,未输入时,使用底部边框显示占位。参数为:底边颜色、底边宽度、底边高度
39+
case PlainTextWithBottomBorder(borderColor: UIColor, borderWidth: CGFloat, borderHeight: CGFloat)
40+
41+
//密文文本(输后显示小圆点),未输入时,使用底部边框显示占位。参数为:底边颜色、底边宽度、底边高度
42+
case PasswordTextWithBottomBorder(borderColor: UIColor, borderWidth: CGFloat, borderHeight: CGFloat)
43+
44+
//自定义模式,需要提供 ABCPinTextFieldInputCustomDraw 类型的毁掉函数
45+
case CustomMode(customDraw: ABCPinTextFieldInputCustomDraw?)
46+
}
47+
48+
//配置类
49+
public class ABCPinTextFieldConfig {
50+
//输入框配置项
51+
public let InputConfig = ABCPinTextFieldInputConfig()
52+
53+
//输入框的个数,默认是4个
54+
public var PinCount: Int = 4
55+
56+
//每个输入框之间的间隔,模式是10
57+
public var InputPadding: CGFloat = 10
58+
59+
//输入框模式,请参考上面的枚举
60+
public var Mode: ABCPinMode = .PlainTextWithDash(dashColor: UIColor.black, dashWidth: 40, dashHeight: 2)
61+
}
62+
63+
//input的配置类 ABCPinTextFieldInputConfig
64+
public class ABCPinTextFieldInputConfig {
65+
//是否显示由控件提供的AccessoryBar
66+
public var ShowAccessoryBar = true
67+
68+
//AccessoryBar的背景色
69+
public var AccessoryBarColor = UIColor(red: 0xe5/255, green: 0xe5/255, blue: 0xe5/255, alpha: 1)
70+
71+
//AccessoryBar的高度
72+
public var AccessoryBarHeight:CGFloat = 120 / UIScreen.main.scale
73+
74+
//AccessoryBar的按钮文本
75+
public var AccessoryBarButtonText = "完成"
76+
77+
//AccessoryBar的按钮文本颜色
78+
public var AccessoryBarButtonTextColor = UIColor.black
79+
80+
//AccessoryBar的按钮文本字体
81+
public var AccessoryBarButtonFont = UIFont.systemFont(ofSize: 30 / UIScreen.main.scale)
82+
83+
//用户提供的AccessoryBar
84+
public var CustomAccessoryView: UIView? = nil
85+
86+
//默认键盘
87+
public var KeyboardType: UIKeyboardType = .numberPad
88+
}
89+
90+
```
91+
92+
**ABCPinTextFieldInput 的 接口**:
93+
```swift
94+
//初始化,直接给出frame参数
95+
public init(with config: ABCPinTextFieldConfig, frame: CGRect)
96+
97+
//初始化,未指定frame参数,需要手动调用Layout方法以进行布局操作
98+
public init(with config: ABCPinTextFieldConfig)
99+
100+
//进行布局操作,如果布局失败,返回false(仅在当前控件的frame是empty的时候,会返回false)
101+
public func Layout() -> Bool
102+
103+
//输入框的代理
104+
public weak var Delegate: ABCPinTextFieldDelegate?
105+
106+
//输入框的使能/禁用
107+
public var IsEnable: Bool {get set}
108+
109+
```
110+
111+
112+
**几个简单的代理**
113+
```swift
114+
@objc public protocol ABCPinTextFieldDelegate: class {
115+
//禁用和使能之后被调用
116+
@objc optional func Enabled()
117+
@objc optional func Disabled()
118+
119+
//所有位置都输入完毕,Resign的时候,该代理方法会被调用
120+
@objc optional func FinishedResign()
121+
122+
//位置没有被填满的时候Resign,该代理方法会被调用
123+
@objc optional func UnfinishedResign()
124+
}
125+
```
126+
127+
128+
### 例子
129+
**ABCPinTextFieldInput** 非常简单。我提供了一个粗糙的Example。
130+
你可以把SourceCode和Example都拖到工程里,然后在ViewController.swift里添加如下代码:
131+
```swift
132+
override func viewDidLoad() {
133+
.... other code
134+
let _ = ABCPinTextFieldExample(view: view)
135+
}
136+
```
137+
138+
139+
140+
### License
141+
MIT

0 commit comments

Comments
 (0)