GithubHelp home page GithubHelp logo

stanislavcekunov / anyformatkit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from luximetr/anyformatkit

0.0 2.0 0.0 911 KB

Simple text formatting in Swift

License: MIT License

Objective-C 0.51% Ruby 0.72% Swift 98.60% Shell 0.16%

anyformatkit's Introduction

AnyFormatKit: Simple text formatting in Swift

CI Status Version Carthage Compatible License Platform Swift

Text formatting framework written on Swift 4.0.

Features

Features
๐ŸŽญ Convert string into formatted string and vice versa
๐Ÿšด Formatting text during typing
#๏ธโƒฃ Set format using '#' characters like '### ##-###'
๐Ÿ“Ž Set prefix for editing string
๐Ÿ™ˆ Symbols input control with RegEx
๐ŸŒ Works with textField/textView/etc with common interface
๐ŸŽ Ready to use subclasses of UITextField and UITextView
๐Ÿ” Allows easy to set attributes for string in range

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+

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

Carthage

To integrate AnyFormatKit into your Xcode project using Carthage, specify it in your Cartfile:

github "luximetr/AnyFormatKit" ~> 0.1.1

Run carthage update to build the framework and drag the built AnyFormatKit.framework into your Xcode project.

Usage

Import

import AnyFormatKit

Formatting with TextFormatter

let phoneFormatter = TextFormatter(textPattern: "### (###) ###-##-##")
phoneFormatter.formattedText(from: "+123456789012") // +12 (345) 678-90-12

let customFormatter = TextFormatter(textPattern: "###-###custom###-###")
customFormatter.formattedText(from: "111222333444") // 111-222custom333-444

You can also set your own symbol in the pattern

let cardFormatter = TextFormatter(textPattern: "XXXX XXXX XXXX XXXX", patternSymbol: "X")
cardFormatter.formattedText(from: "4444555566667777") // 4444 5555 6666 7777

For string with different length

let formatter = TextFormatter(textPattern: "## ###-##")
formatter.formattedText(from: "1234") // 12 34
formatter.formattedText(from: "123456789") // 12 345-67

Unformatting

let formatter = TextFormatter(textPattern: "## ###-##")
formatter.unformattedText(from: "99 888-77") // 9988877

Formatting during typing

It is necessary to create TextInputController instance with formatter for formatting during typing. You need to implement TextInput protocol in your own UITextField/UITextView/something else or use ready solutions (TextInputField/TextInputView) by subclassing. It is necessary to set controllers's textInput property.

let textInputController = TextInputController()

let textInput = TextInputField() // or TextInputView or any TextInput
textInputController.textInput = textInput // setting textInput

let formatter = TextInputFormatter(textPattern: "### (###) ###-##-##", prefix: "+12")
textInputController.formatter = formatter // setting formatter

The controller listens textInput(_:shouldChangeCharactersIn:replacementString:) delegate method. But you can also add more than one delegate if needed. Methods of the delegates, that should return Bool value gather using && operator. Therefore, if one of the delegates returns false, that means that textInput will receive false. If you want to send true to textInput, all delegates must return true.

You can set allowedSymbolsRegex to the formatter to filter input symbols with the RegEx. All symbols, that satisfy the RegEx will be available for typing in the textInput. This property only applies to inputed symbols from the keyboard, but not to the prefix.

inputFieldFormatter.allowedSymbolsRegex = "[0-9]" // allowed only numbers

Attributes for range

To set attributes for string at range use addAttributes(_:range:) method for textInput.

textInput.addAttributes([.foregroundColor : UIColor.lightGray], range: NSRange(location: 0, length: 3))

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

Watchers

James Cloos avatar  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.