AKMaskField
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.
Features
- 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
Requirements
- iOS 8.0+
- Xcode 7.3+
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. To install AKMaskField with CocoaPods:
-
Make sure CocoaPods is installed.
-
Update your Podfile to include the following:
use_frameworks!
pod 'AKMaskField' -
Run
pod install.
- In your code import AKMaskField like so:
import AKMaskField
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To install AKMaskField with Carthage:
-
Install Carthage via Homebrew
$ brew update
$ brew install carthage -
Add
github "artemkrachulov/AKMaskField"to your Cartfile. -
Run
carthage update. -
Drag
AKMaskField.frameworkfrom theCarthage/Build/iOS/directory to theLinked Frameworks and Librariessection of your Xcode project'sGeneralsettings. -
Add
$(SRCROOT)/Carthage/Build/iOS/AKMaskField.frameworktoInput Filesof Run Script Phase for Carthage.
Manual
If you prefer not to use either of the aforementioned dependency managers, you can integrate AKMaskField into your project manually.
- Download and drop AKMaskField folder in your project.
- Done!
Usage example
Storyboard
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:
- MaskExpression: {dddd}-{DDDD}-{WaWa}-{aaaa}
- MaskTemplate: ABCD-EFGH-IJKL-MNOP
Programmatically
Setup mask field in your view controller.
override func viewDidLoad() {
super.viewDidLoad()
field = AKMaskField()
field.maskExpression = "{dddd}-{DDDD}-{WaWa}-{aaaa}"
field.maskTemplate = "ABCD-EFGH-IJKL-MNOP"
}
Configuring the Mask Field
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.
The 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 *.
Use this method to set the mask and template parameters.
Parameters
maskExpression: Mask (read above).maskTemplate: Mask template (read above).
You can also set default
placeholderproperty. The placeholder will shows only when mask field is clear.
Open and close bracket character for the mask block.
Default value of this property is { and }.
Setup Mask Field behavior
Jumps to previous block when cursor placed between brackets or before first character in current block.
Default value of this property is false.
Accessing the Text Attributes
The text displayed by the mask field.
Mask Field actions
Manually refresh the mask field
Accessing the Delegate
The receiver's delegate.
Getting the Mask Field status
Returns the current status of the mask field. The value of the property is a constant. See AKMaskFieldStatus for descriptions of the possible values.
Getting the Mask Field object
Returns an array containing all the Mask Field blocks
AKMaskFieldDelegate
Managing Editing
Asks the delegate if editing should begin in the specified mask field.
Parameters
maskField: The mask field in which editing is about to begin.
Asks the delegate if editing should begin in the specified mask field.
Parameters
maskField: The mask field in which editing is about to begin.
Asks the delegate if editing should stop in the specified mask field.
Parameters
maskField: The mask field in which editing is about to end.
Tells the delegate that editing stopped for the specified mask field.
Parameters
maskField: The mask field for which editing ended.
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.
Editing the Mask Field's Block
Asks 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).
Structures
AKMaskFieldBlock
A structure that contains the mask block main properties.
General
Block index in the mask
Returns the current block status.
An array containing all characters inside block. See AKMaskFieldBlockCharacter structure information.
Pattern
The mask pattern that represent current block.
Location of the mask pattern in the mask.
Mask template
The mask template string that represent current block.
Location of the mask template string in the mask template.
AKMaskFieldBlockCharacter
A structure that contains the block character main properties.
General
Character index in the block.
The block index in the mask.
Current character status.
Pattern
The mask pattern character. See AKMaskFieldPatternCharacter costant information.
Location of the pattern character in the mask.
Mask template
The mask template character.
Location of the mask template character in the mask template.
Constants
AKMaskFieldStatus
case Clear
case Incomplete
case Complete
}
The Mask Field, Block and Block Character status property constant.
AKMaskFieldEvent
case None
case Insert
case Delete
case Replace
}
Event constant value received after manipulations with the Mask Field.
AKMaskFieldPatternCharacter
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)
Returns regular expression pattern.
Contribute
Please do not forget to this repository to increases its visibility and encourages others to contribute.
Got a bug fix, or a new feature? Create a pull request and go for it!
Meta
Artem Krachulov - www.artemkrachulov.com - artem.krachulov@gmail.com
Released under the MIT license