Giter Club home page Giter Club logo

anyformatkit's Introduction

AnyFormatKit: Simple text formatting in Swift

CI Status Version Carthage Compatible License Platform Swift

Text formatting framework written on Swift 5.0.

Features

Features
๐ŸŽญ Convert string into formatted string and vice versa
๐Ÿšด Formatting text during typing
#๏ธโƒฃ Set format using '#' characters like '### ##-###'

Example

To run the example project, clone the repo and run pod install from the Example directory first.

Phone number example

AnyFormatKitDemo: Simple text formatting in Swift

Currency example

AnyFormatKitCurrencyDemo: Currency formatting in Swift

Requirements

  • iOS 8.0+
  • Swift 4.0+
  • Xcode 9.0+

Migration Guides

Installation

CocoaPods

AnyFormatKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'AnyFormatKit'

Then, run the following command:

$ pod install

Usage

Import

import AnyFormatKit

Formatting with TextFormatter

let phoneFormatter = DefaultTextFormatter(textPattern: "### (###) ###-##-##")
phoneFormatter.format("+123456789012") // +12 (345) 678-90-12

let customFormatter = DefaultTextFormatter(textPattern: "###-###custom###-###")
customFormatter.format("111222333444") // 111-222custom333-444

You can also set your own symbol in the pattern

let cardFormatter = DefaultTextFormatter(textPattern: "XXXX XXXX XXXX XXXX", patternSymbol: "X")
cardFormatter.format("4444555566667777") // 4444 5555 6666 7777

For string with different length

let formatter = DefaultTextFormatter(textPattern: "## ###-##")
formatter.format("1234") // 12 34
formatter.format("123456789") // 12 345-67

Unformatting

let formatter = DefaultTextFormatter(textPattern: "## ###-##")
formatter.unformat("99 888-77") // 9988877

Formatting during typing

Code from example app

let formatter = DefaultTextInputFormatter(textPattern: "### (###) ###-##-##")

// inside of UITextFieldDelegate shouldChangeTextIn method
let result = formatter.formatInput(currentText: textView.text, range: range, replacementString: text)
textView.text = result.formattedText
textView.setCursorLocation(result.caretBeginOffset)

Author

luximetr, [email protected]

License

AnyFormatKit is available under the MIT license. See the LICENSE file for more info.

anyformatkit's People

Contributors

luximetr avatar jvanzummeren avatar luximetreducation avatar twodollarsesq avatar caramell0 avatar jimvanzummeren avatar devcarlos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.