Latest 1.0.0
Homepage https://github.com/skladek/SKStringFormatter
License MIT
Platforms ios 9.0
Authors

Travis Status
Codecov Status
Pod Version
Platform Status
License Status

SKStringFormatter is a formatter to visually style strings to a known structure while maintaining the raw value of a string without the style additions. This can be used to add formatting for a phone number for example. Check out the SampleProject in the workspace to see some usage examples.


Installation

Cocoapods

Instalation is supported through Cocoapods. Add the following to your pod file for the target where you would like to use SKStringFormatter:

pod 'SKStringFormatter'

Initialization

To use the string formatter, it must be initialized with an object conforming to the StringFormat protocol. This protocol defines attributes about how the format should be applied.

import SKStringFormatter
let stringFormatter = StringFormatter(stringFormat: stringFormat)

StringFormat

This protocol defines attributes about how a format should be applied.

allowedCharacterSet

If this is set, only characters in the allowedCharacterSet will be allowed. Any characters not in the set will be discarded. If this is nil, all characters are allowed.

formatStrings

An array of FormatString objects which define the position and other attributes of static characters. For instance, the dashes in a Social Security Number format (XXX-XX-XXXX) would be defined with format strings.

maxLength

The maximum length of the format. Any characters beyond the max length will be discarded.


FormatString

FormatString objects define the static decoration strings within a string format. The parenthesis and dash in a US phone number format ((XXX) XXX-XXXX) would be defined with FormatString objects.

Initialization

A FormatString is initialized by providing the string to display (string), position for the string (startIndex), and optionally, the number of characters that the string must reach before displaying the display string (displaysAt).

FormatString(string: "(", startIndex: 0, displaysAt: 4)

In the above example, the "(" character will be inserted at character index 0 when the string to be formatted reaches 4 characters in length. Note, both the startIndex and displaysAt values do not count any FormatStrings displayed. The indexes only count characters entered by the user.


UITextFieldDelegate

The StringFormatter object implements UITextFieldDelegate methods to handle live updating of the format as the user inputs text. Simply set the StringFormatter as the UITextFieldDelegate to adopt this behavior.

textfield.delegate = stringFormatter

FormattedTextField

A UITextField subclass is provided that can be used in conjunction with the StringFormatter. The FormattedTextField prevents user interaction with the field. The user cannot select, paste, or move the cursor. All text entry is appended to the end of the current entry. This subclass does not need to be used, but it is recommended.

Latest podspec

{
    "name": "SKStringFormatter",
    "version": "1.0.0",
    "license": "MIT",
    "summary": "A flexible formatter for strings.",
    "homepage": "https://github.com/skladek/SKStringFormatter",
    "authors": {
        "Sean Kladek": "[email protected]"
    },
    "source": {
        "git": "https://github.com/skladek/SKStringFormatter.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "Source/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This