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 and mask template 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
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"
}var mask: String?The string value that has blocks with pattern symbols that determine the certain format of input data. Wrap each mask block with proper bracket character.
The predetermined formats:
| Mask symbol (pattern) | Input format |
|---|---|
| d | Number, decimal number from 0 to 9 |
| D | Any symbol, except decimal number |
| W | Not an alphabetic symbol |
| a | Alphabetic symbol, a-Z |
| . | Corresponds to any symbol (default) |
Default value of this property is nil.
var maskTemplate: StringThe text that represents the mask filed with replacing mask symbol by template character.
| Characters count | Input format |
|---|---|
| 1 | Template character will be copied to each mask block with repeating equal block length. |
| Equal | Template length equal to mask without brackets. Template characters will replace mask blocks in same range. |
Default value of this property is *.
func setMask(mask: String, withMaskTemplate maskTemplate: String)Use this method to set the mask and template parameters.
Parameters
mask: Mask (read above).maskTemplate: Mask template (read above).
You can also set default
placeholderproperty. The placeholder will shows only when mask field is clear.
var maskBlockBrackets: AKMaskFieldBracketsOpen and close bracket character for the mask block.
Default value of this property is { and }.
public var text: String?The text displayed by the mask field.
func refreshMask()Manually refresh the mask field
weak var maskDelegate: AKMaskFieldDelegate?The receiver’s delegate.
var maskStatus: AKMaskFieldStatus { get }Returns the current status of the mask field. The value of the property is a constant. See AKMaskFieldStatus for descriptions of the possible values.
var maskBlocks: [AKMaskFieldBlock] { get }Returns an array containing all the Mask Field blocks
optional func maskFieldShouldBeginEditing(maskField: AKMaskField) -> BoolAsks the delegate if editing should begin in the specified mask field.
Parameters
maskField: The mask field in which editing is about to begin.
optional func maskFieldDidBeginEditing(maskField: AKMaskField)Asks the delegate if editing should begin in the specified mask field.
Parameters
maskField: The mask field in which editing is about to begin.
optional func maskFieldShouldEndEditing(maskField: AKMaskField) -> BoolAsks the delegate if editing should stop in the specified mask field.
Parameters
maskField: The mask field in which editing is about to end.
optional func maskFieldDidEndEditing(maskField: AKMaskField)Tells the delegate that editing stopped for the specified mask field.
Parameters
maskField: The mask field for which editing ended.
optional func maskField(maskField: AKMaskField, didChangedWithEvent event: AKMaskFieldEvent)Tells the delegate that specified mask field change text with event.
Parameters
maskField: The mask field for which event changed.event: Event constant value received after manipulations.
optional func maskField(maskField: AKMaskField, shouldChangeBlock block: AKMaskFieldBlock, inout inRange range: NSRange, inout replacementString string: String) -> BoolAsks the delegate if the specified mask block should be changed.
Parameters
maskField: The mask field containing the text.block: Target block. See ** AKMaskFieldBlock** more information.range: The range of characters to be replaced (inout parameter).string: The replacement string for the specified range (inout parameter).
A structure that contains the mask block main properties.
General
var index: IntBlock index in the mask
var status: AKMaskFieldStatus { get }Returns the current block status.
var chars: [AKMaskFieldBlockCharacter]An array containing all characters inside block. See AKMaskFieldBlockCharacter structure information.
Pattern
var pattern: String { get }The mask pattern that represent current block.
var patternRange: NSRange { get }Location of the mask pattern in the mask.
Mask template
var template: String { get }The mask template string that represent current block.
var templateRange: NSRange { get }Location of the mask template string in the mask template.
A structure that contains the block character main properties.
General
var index: IntCharacter index in the block.
var blockIndex: IntThe block index in the mask.
var status: AKMaskFieldStatusCurrent character status.
Pattern
var pattern: AKMaskFieldPatternCharacterThe mask pattern character. See AKMaskFieldPatternCharacter costant information.
var patternRange: NSRangeLocation of the pattern character in the mask.
Mask template
var template: CharacterThe mask template character.
var templateRange: NSRangeLocation of the mask template character in the mask template.
enum AKMaskFieldStatus {
case Clear
case Incomplete
case Complete
}The Mask Field, Block and Block Character status property constant.
enum AKMaskFieldEvet {
case None
case Insert
case Delete
case Replace
}Event constant value received after manipulations with the Mask Field.
enum AKMaskFieldPatternCharacter: String {
case NumberDecimal = "d"
case NonDecimal = "D"
case NonWord = "W"
case Alphabet = "a"
case Any = "."
}Single block character pattern constant.
Constatns
NumberDecimal: Number, decimal number from 0 to 9NonDecimal: Any symbol, except decimal numberNonWord: Not an alphabetic symbolAlphabet: Alphabetic symbol, a-ZAny: Corresponds to any symbol (default)
func pattern() -> StringReturns regular expression pattern.
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
