A Swift Formatter Kit

Related tags

Text Format
Overview

Format - a Swift Formatter Kit

Build Status Version Carthage compatible

Format

A Swift formatter kit. Simple formatting syntax for decimal numbers, currency, mass, addresses, ordinal numbers and hexadecimal colors.

Usage

Import Format at the top of the Swift file with the content you would like to format.

import Format

Number Formatting

Format provides a formatting extension for all number types. To format an Int to two decimal places:

let formattedNumber = 45.format(Decimals.two) // 45.00

Format defaults to the user's current locale but a custom locale can be easily provided:

let frLocale = Locale(identifier: "FR")
let gbLocale = Locale(identifier: "GB")
let formattedFRNumber = 99.format(Currency.EUR, locale: frLocale) // 99,00 €
let formattedGBNumber = 99.format(Currency.GBP, locale: gbLocale) // £ 99.00

Apply any of these formatters to any number type:

Decimals.three // 10.123
Currency.USD // $10.12
General.ordinal // 10th (iOS9+ only)
General.spellOut // ten point one two three
General.distance // 30 feet
Mass.person // 67 kg

The distance formatter assumes the number represents the distance in meters before converting and formatting it to the current locale's preferred unit.

Address Formatting (iOS9.0+ only)

Different cultures have different ways of displaying addresses. Format includes an extension on CLPlacemark that converts the addressDictionary to a formatted string in the current locale:

let address = placemark.format()

Please note that this function will produce a deprecated warning when used. This is because Apple is using AddressBook keys in the CLPlacemark and AddressBook was deprecated.

To format a custom address (all fields are optional strings):

let address = AddressFormatter().format(street, city: city, state: state, postalCode: postalCode, country: country, ISOCountryCode: ISOCountryCode)

Color Formatting

Format can help you convert hexadecimal colors from the web to UIColors you can work with:

let color = ColorFormatter().format("2ba134")

In case of an error, the color will default to black if the string is empty or white if the string is invalid.

Setting up with Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

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

github "marmelroy/Format"

Setting up with CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
pod 'Format', '~> 0.5'
Comments
  • Add support for the currency bitcoin

    Add support for the currency bitcoin

    opened by SandroMachado 3
  • Protocol extension on Numbers & Simpler Enums

    Protocol extension on Numbers & Simpler Enums

    • [x] Implemented a NumberFormatProvider to avoid code duplication and have something more elegant :trollface:
    • [x] Simplified Currency and Decimals enums by using rawValues, this allows us to have a simpler, shorter and more maintainable enums.

    t0xhtgj

    opened by alikaragoz 1
  • Feat: number separator

    Feat: number separator

    Why?

    To format numbers with given separators and spacing.

    Changes

    • Add separator NumberFormatterType
    • Format numbers with a given separator and spacing
    • Remove unnecessary NumberFormat singleton

    Related to issue

    I've followed the main used patterns I hope to be contributing :)

    opened by marinofelipe 1
  • NSNumber' is not convertible to 'CLLocationDistance'

    NSNumber' is not convertible to 'CLLocationDistance'

    Hello, i have an issue after install: in this function,

    open func format(_ number: NSNumber, formatter: NumberFormatterCustomLocaleAvailable, locale: Locale) -> String

    let distance = number as CLLocationDistance

    NSNumber' is not convertible to 'CLLocationDistance' (aka 'Double'); did you mean to use 'as!' to force downcast?

    xCode Version 9.3.1 (9E501) platform :ios, '10.0' swift_version = "4.0"

    opened by mueslifante 2
  • Wrong when change language.

    Wrong when change language.

    Hi @marmelroy, thanks for greate Format, when i change from US language to Vietnamese, i have bug: $ -> US$. I use this function: .format(Format.Currency(rawValue:USD)). Many thank for support.

    opened by mhtranbn 0
Releases(0.5.0)
Owner
Roy Marmelstein
Ya tu sabes.
Roy Marmelstein
UITextField realtime currency formatter - format while typing

Objective-C Realtime Currency Formatter An extremely simple Objective-C class for formatting currencies. Ready to work with the UITextFieldDelegate an

Kaio Medau 14 Jul 28, 2022
VEditorKit - Lightweight and Powerful Editor Kit built on Texture(AsyncDisplayKit)

VEditorKit provides the most core functionality needed for the editor. Unfortunately, When combined words are entered then UITextView selectedRange will changed and typingAttribute will cleared. So, In combined words case, Users can't continue typing the style they want.

David Ha 471 Dec 27, 2022
BonMot is a Swift attributed string library

BonMot (pronounced Bon Mo, French for good word) is a Swift attributed string library. It abstracts away the complexities of the iOS, macOS, tvOS, and

Rightpoint 3.4k Dec 30, 2022
Croc is a swift emoji string parsing library

Croc is a library for parsing emojis on iOS. It provides a simple and lightweight interface for detecting, generating, categorizing and managing emoji characters, making emoji-powered features an easy task for developers.

Joe Kalash 127 Nov 20, 2022
Fully open source text editor for iOS written in Swift.

Edhita Fully open source text editor for iOS written in Swift. http://edhita.bornneet.com/ What Edhita means? Edhita (Romaji) == エディタ (Katakana) == Ed

Tatsuya Tobioka 1.2k Jan 1, 2023
A simple and customizable Markdown Parser for Swift

MarkdownKit MarkdownKit is a customizable and extensible Markdown parser for iOS and macOS. It supports many of the standard Markdown elements through

Bruno Oliveira 687 Dec 18, 2022
Marky Mark is a parser written in Swift that converts markdown into native views.

Marky Mark is a parser written in Swift that converts markdown into native views. The way it looks it highly customizable and the supported markdown syntax is easy to extend.

M2mobi 287 Nov 29, 2022
Swift Parser Combinators

Parsey Swift Parser Combinator Framework In addition to simple combinators, Parsey supports source location/range tracking, backtracking prevention, a

Richard Wei 56 Jun 30, 2022
Great Swift String Pluralize Extension

Pluralize.swift Great Swift String Pluralize Extension case-insensitive tons of rules for irregular nouns (plural form) supports uncountable nouns all

Joshua Arvin Lat 193 Nov 8, 2022
An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.

PrediKit A Swift NSPredicate DSL for iOS & OS X inspired by SnapKit, lovingly written in Swift, and created by that weird dude at KrakenDev. If you're

Hector Matos 542 Sep 24, 2022
PySwiftyRegex - Easily deal with Regex in Swift in a Pythonic way

PySwiftyRegex Easily deal with Regex in Swift in a Pythonic way.

Ce Zheng 232 Oct 12, 2022
Regular expressions for swift

Regex Advanced regular expressions for Swift Goals Regex library was mainly introduced to fulfill the needs of Swift Express - web application server

Crossroad Labs 328 Nov 20, 2022
👩‍🎨 Elegant Attributed String composition in Swift sauce

Elegant Attributed String composition in Swift sauce SwiftRichString is a lightweight library which allows to create and manipulate attributed strings

Daniele Margutti 2.9k Jan 5, 2023
SwiftVerbalExpressions is a Swift library that helps to construct difficult regular expressions

SwiftVerbalExpressions Swift Regular Expressions made easy SwiftVerbalExpressions is a Swift library that helps to construct difficult regular express

null 582 Jun 29, 2022
A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.

PhoneNumberKit Swift 5.3 framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber. Features F

Roy Marmelstein 4.7k Jan 8, 2023
A comprehensive, lightweight string extension for Swift

SwiftString SwiftString is a lightweight string extension for Swift. This library was motivated by having to search StackOverflow for common string op

Andrew Mayne 1.6k Dec 30, 2022
🌭 Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it.

Mustard ?? Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it. Quick start using character sets Foundation

Mathew Sanders 695 Nov 11, 2022
Automatic summarizer text in Swift

Overview Reductio is a tool used to extract keywords and phrases using an implementation of the algorithm TextRank. Installation Swift Package Manager

Sergio Fernández 438 Dec 9, 2022
OysterKit is a framework that provides a native Swift scanning, lexical analysis, and parsing capabilities. In addition it provides a language that can be used to rapidly define the rules used by OysterKit called STLR

OysterKit A Swift Framework for Tokenizing, Parsing, and Interpreting Languages OysterKit enables native Swift scanning, lexical analysis, and parsing

Swift Studies 178 Sep 16, 2022