Home

Awesome

<p align="center"> <img src="https://raw.githubusercontent.com/delba/TextAttributes/assets/TextAttributes@2x.png"> </p> <p align="center"> <a href="https://travis-ci.org/delba/TextAttributes"><img alt="Travis Status" src="https://img.shields.io/travis/delba/TextAttributes.svg"/></a> <a href="https://img.shields.io/cocoapods/v/TextAttributes.svg"><img alt="CocoaPods compatible" src="https://img.shields.io/cocoapods/v/TextAttributes.svg"/></a> <a href="https://github.com/Carthage/Carthage"><img alt="Carthage compatible" src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat"/></a> <a href="https://img.shields.io/cocoapods/p/TextAttributes.svg"><img alt="Platform" src="https://img.shields.io/cocoapods/p/TextAttributes.svg"/></a> </p>

TextAttributes makes it easy to compose attributed strings.

let attrs = TextAttributes()
    .font(name: "HelveticaNeue", size: 16)
    .foregroundColor(white: 0.2, alpha: 1)
    .lineHeightMultiple(1.5)

NSAttributedString(string: "The quick brown fox jumps over the lazy dog", attributes: attrs)
<p align="center"> <a href="#features">Features</a> • <a href="#usage">Usage</a> • <a href="#demo">Demo</a> • <a href="#references">References</a> • <a href="#installation">Installation</a> • <a href="#license">License</a> </p>

Features

Usage

attrs.font = UIFont(name: "HelveticaNeue", size: 16)
attrs.backgroundColor = .white

See all the properties

The TextAttributes methods return Self to allow method chaining:

attrs
    .lineHeightMultiple(1.5)
    .underlineStyle(.styleSingle)

See all the methods

The following are equivalent:

attrs
    .font(name: "HelveticaNeue", size: 16)
    .foregroundColor(white: 0.2, alpha: 1)
let font  = UIFont(name: "HelveticaNeue", size: 16)
let color = UIColor(white: 0.2, alpha: 1)

attrs
    .font(font)
    .foregroundColor(color)
attrs.dictionary // Returns the attributes dictionary of type [NSAttributedString.Key: Any]

Third-party libraries:

Demo

<p align="center"> <img src="https://raw.githubusercontent.com/delba/TextAttributes/assets/demo.gif" /> <em>Live updates with <a href="https://github.com/johnno1962/injectionforxcode" alt="Injection for Xcode">Injection for Xcode</a></em> </p>

References

Attributes dictionary

Dictionary KeyTextAttributes PropertyTextAttributes Method
NSFontAttributeNamefontfont(name:size:)
font(_:)
NSParagraphStyleAttributeNameparagraphStyleparagraphStyle(_:)
NSForegroundColorAttributeNameforegroundColorforegroundColor(_:)
foregroundColor(white:alpha:)
foregroundColor(hue:saturation:brightness:alpha:)
foregroundColor(red:green:blue:alpha:)
foregroundColor(patternImage:)
NSBackgroundColorAttributeNamebackgroundColorbackgroundColor(_:)
backgroundColor(white:alpha:)
backgroundColor(hue:saturation:brightness:alpha:)
backgroundColor(red:green:blue:alpha:)
backgroundColor(patternImage:)
NSLigatureAttributeNameligatureligature(_:)
NSKernAttributeNamekernkern(_:)
NSStrikethroughStyleAttributeNamestrikethroughStylestrikethroughStyle(_:)
NSStrikethroughColorAttributeNamestrikethroughColorstrikethroughColor(_:)
strikethroughColor(white:alpha:)
strikethroughColor(hue:saturation:brightness:alpha:)
strikethroughColor(red:green:blue:alpha:)
strikethroughColor(patternImage:)
NSUnderlineStyleAttributeNameunderlineStyleunderlineStyle(_:)
NSUnderlineColorAttributeNameunderlineColorunderlineColor(_:)
underlineColor(white:alpha:)
underlineColor(hue:saturation:brightness:alpha:)
underlineColor(red:green:blue:alpha:)
underlineColor(patternImage:)
NSStrokeWidthAttributeNamestrokeWidthstrokeWidth(_:)
NSStrokeColorAttributeNamestrokeColorstrokeColor(_:)
strokeColor(white:alpha:)
strokeColor(hue:saturation:brightness:alpha:)
strokeColor(red:green:blue:alpha:)
strokeColor(patternImage:)
NSShadowAttributeNameshadowshadow(_:)
shadow(color:offset:blurRadius:)
NSTextEffectAttributeNametextEffecttextEffect(_:)
NSAttachmentAttributeNameattachmentattachment(_:)
NSLinkAttributeNamelinklink(_:)
link(string:)
link(string:relativeToURL:)
NSBaselineOffsetAttributeNamebaselineOffsetbaselineOffset(_:)
NSObliquenessAttributeNameobliquenessobliqueness(_:)
NSExpansionAttributeNameexpansionexpansion(_:)
NSVerticalGlyphFormAttributeNameverticalGlyphFormverticalGlyphForm(_:)

Paragraph style

NSMutableParagraphStyle PropertyTextAttributes PropertyTextAttributes Method
alignmentalignmentalignment(_:)
firstLineHeadIndentfirstLineHeadIndentfirstLineHeadIndent(_:)
headIndentheadIndentheadIndent(_:)
tailIndenttailIndenttailIndent(_:)
lineBreakModelineBreakModelineBreakMode(_:)
maximumLineHeightmaximumLineHeightmaximumLineHeight(_:)
lineSpacinglineSpacinglineSpacing(_:)
paragraphSpacingparagraphSpacingparagraphSpacing(_:)
paragraphSpacingBeforeparagraphSpacingBeforeparagraphSpacingBefore(_:)
baseWritingDirectionbaseWritingDirectionbaseWritingDirection(_:)
lineHeightMultiplelineHeightMultiplelineHeightMultiple(_:)

Installation

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 TextAttributes into your Xcode project using Carthage, specify it in your Cartfile:

github "delba/TextAttributes"

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

You can install it with the following command:

$ gem install cocoapods

To integrate TextAttributes into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

pod 'TextAttributes'

License

Copyright (c) 2016-2019 Damien (http://delba.io)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.