Coder Social home page Coder Social logo

format's Introduction

Format - a Swift Formatter Kit

Build Status Version Carthage compatible

Format

A Swift 2.0 formatter kit. Simple formatting syntax for decimal numbers, currency, 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 = NSLocale(localeIdentifier: "FR")
let gbLocale = NSLocale(localeIdentifier: "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
General.SpellOut // ten point one two three
General.Distance // 30 feet

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

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.1'

format's People

Contributors

marmelroy avatar

Watchers

Chew Chit Siang avatar 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.