AKMaskField is UITextField subclass which allows enter data in the fixed quantity and in the certain format (credit cards, telephone numbers, dates, etc.). You only need setup mask string and mask template string visible for user.
- Easy in use
- Possibility to setup input field from a code or Settings Panel
- Smart template
- Support of dynamic change of a mask
- Fast processing of a input field
- Smart copy / insert action
- iOS 8.0+
- Xcode 7.3+
- Clone or download demo project.
- Add the AKMaskField folder to your project (To copy the file it has to be chosen).
Create a text field UITextField
and set a class AKMaskField
in the Inspector / Accessory Panel tab. Specify necessary attributes in the Inspector / Accessory Attributes tab.
Example:
- Mask: {dddd}-{DDDD}-{WaWa}-{aaaa}
- Mask Template: ABCD-EFGH-IJKL-MNOP
Drag field to view controller class. Create outlet.
@IBOutlet weak var field: AKMaskField!
Setup mask field in your view controller.
var field: AKMaskField!
override func viewDidLoad() {
super.viewDidLoad()
field = AKMaskField()
field.mask = "{dddd}-{DDDD}-{WaWa}-{aaaa}"
field.maskTemplate = "ABCD-EFGH-IJKL-MNOP"
}
// Output
// ABCD-EFGH-IJKL-MNO
You can also set other properties like
text
,placeholder
, etc. Example if you will settext
property to value1234
. A mask field will show0123-EFGH-IJKL-MNOP
after loading view controller. Or ifplaceholder
property will be not empty you will see placeholder text (oly when mask field is clear).
func updateText(text: String?)
Will update text in the mask field. Parameters:
text
: The text, that you want to display by the mask field.
This property updated manually when user type anything in the mask field.
var mask: String?
The string value that contains blocks with symbols that determines certain format of input data. Each block must be wrapped in brackets. Default brackets value is { ... }
.
The predetermined formats:
d
: Number, decimal number from 0 to 9D
: Any symbol, except decimal numberW
: Not an alphabetic symbola
: Alphabetic symbol, a-Z.
: Corresponds to any symbol (default)
This property is empty by default.
var maskTemplate: String
The text that represents the mask filed with replacing format symbol with template characters. Can be set (characters count):
1
: This character will be copied in each block and will replace mask format symbol.Same length as mask without brackets
: Template character will replace mask format symbol in same position.
The initial value of this property is *
.
func setMask(mask: String, withMaskTemplate maskTemplate: String!)
Set up mask field mask field. Parameters listed before.
var maskBlockBrackets: AKMaskFieldBrackets
Two characters (opening and closing bracket for the block mask).
The initial values is {
and }
.
weak var maskDelegate: AKMaskFieldDelegate?
A mask field delegate responds to editing-related messages from the mask field. All delegate methods listed below.
var maskObject: [AKMaskFieldBlock]? {get}
An array with all mask blocks mask defined as AKMaskFieldBlock
structure.
struct AKMaskFieldBlock {
var index: Int
var status: Bool
var range: Range<Int>
var mask: String
var template: String
var chars: [AKMaskFieldBlockChars]
}
index
: Block position number in the maskstatus
: Current block complete statusrange
: Block range in the mask (without brackets)mask
: Mask characters inside this block between bracketstemplate
: Mask template placeholder corresponding mask characters inside this blockchars
: Characters list with parameters
AKMaskFieldBlockChars
structure.
struct AKMaskFieldBlockChars {
var index: Int
var status: Bool
var range: Range<Int>
var text: Character?
}
index
: Character position number in the mask blockstatus
: Current character complete statusrange
: Character range in the mask (without brackets)text
: Current character
The initial value of this property is nil
.
var maskStatus: AKMaskFieldStatus
Current status of the mask field representes as AKMaskFieldStatus
enumeration type.
enum AKMaskFieldStatus {
case Clear
case Incomplete
case Complete
}
Clear
: No one character was enteredIncomplete
: At least one character is not enteredComplete
: All characters was entered
The initial value of this property is Clear
.
optional func maskFieldDidBeginEditing(maskField: AKMaskField)
Tells the delegate that editing began for the specified mask field maskField
.
optional func maskFieldDidEndEditing(maskField: AKMaskField)
Tells the delegate that editing finished for the specified mask field.
optional func maskField(maskField: AKMaskField, didChangeCharactersInRange range: NSRange, replacementString string: String, withEvent event: AKMaskFieldEvent)
Tells the delegate that specified mask field maskField
change text with event.
Parameters:
range
: The range of characters to be replacedstring
: The replacement stringevent
: Defines a user event,AKMaskFieldEvet
enumeration typeenum AKMaskFieldEvet { case None case Insert case Delete case Replace }
Error
: Error with placing new character.Insert
: Entering new textDelete
: Deleting text from fieldReplace
: Selecting and replacing or deleting text
Please do not forget to ★ this repository to increases its visibility and encourages others to contribute.
Artem Krachulov: www.artemkrachulov.com Mail: artem.krachulov@gmail.com
Released under the MIT license