Swiftpack.co - SteveBarnegren/AttributedStringBuilder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by SteveBarnegren.
SteveBarnegren/AttributedStringBuilder 1.2.0
Easily construct attributed strings in swift
⭐️ 66
🕓 14 hours ago
iOS macOS tvOS
.package(url: "https://github.com/SteveBarnegren/AttributedStringBuilder.git", from: "1.2.0")


Version License Platform

AttributedStringBuilder takes the pain out of creating NSAttributedStrings.

  • More readable NSAttributedString creation
  • No more faffing about with attribute dictionaries
  • Easliy render images in strings
  • 'Swifty' api


Import the AttributedStringBuilder module

import AttributedStringBuilder

Creating a string

AttributedStringBuilder functions always return the AttributedStringBuilder instance that you are using, so you can write concise and readable code by chaining function calls together.

Attributes are defined using enum cases with associated values

Default attributes can be set, and overriden for selected text portions.

let builder = AttributedStringBuilder()

builder.defaultAttributes = [
    .font( UIFont.systemFont(ofSize: 16, weight: UIFontWeightRegular) ),

    .text("Attributed strings can make")
    .text("specific words", attributes: [.textColor(UIColor.red)])
    .text("pop out")

Basic Example 1

Another Example

By chaining method calls together, complex AttributedStrings can be created with just a few lines of code

let builder = AttributedStringBuilder()
builder.defaultAttributes = [
    .font( UIFont(name: "AvenirNext-Bold", size: 30)! )

    .text("It's ")
    .text("Easy ", attributes: [.underline(true), .textColor(UIColor.blue)])
    .text("To ", attributes: [.strokeWidth(2), .textColor(UIColor.black)])
    .text("Adjust ", attributes: [.skew(0.3), .textColor(UIColor.magenta)])
    .text("Attributes ", attributes: [.font(UIFont(name: "Baskerville-Bold", size: 30)!)])


Basic Example 2

A More Complex Example

// Create an AttributedStringBuilder with default attributes
let builder = AttributedStringBuilder()
builder.defaultAttributes = [.alignment(.center)]

// First line attributes
let titleAttributes: [AttributedStringBuilder.Attribute] = [
    .font(UIFont(name: "Futura-Bold", size: 40)!),

// Second Line Attributes
let canDoAttributes: [AttributedStringBuilder.Attribute] = [
    .font(UIFont(name: "Marker Felt", size: 30)!),

// Third Line Attributes
let shadow = NSShadow()
shadow.shadowColor = UIColor.black
shadow.shadowBlurRadius = 5

let awesomeAttributes: [AttributedStringBuilder.Attribute] = [
    .font(UIFont(name: "AvenirNext-Bold", size: 40)!),

// Build the string
    .text("ATTRIBUTED STRINGS", attributes: titleAttributes)
    .text("Can do", attributes: canDoAttributes)
    .text("AWESOME THINGS", attributes: awesomeAttributes)


Shadow Example


It's easy to render images in your strings, and to adjust the size to fit the uppercase or loowercase height of the font

let font = UIFont.systemFont(ofSize: 90)
let builder = AttributedStringBuilder()
builder.defaultAttributes = [
let image = UIImage(named: "PurpleMonster")!
    .image(image, withSizeFittingFontUppercase: font)
    .image(image, withSizeFittingFontLowercase: font)

Images Example



Add the following line to your Podfile:

pod "AttributedStringBuilder"

Copy AttributedStringBuilder.swift in to your project




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


Stars: 66
Last commit: 14 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Swift 5 Support
3 years ago

Adds support for Swift 5 (thanks @evgeniymikholap!)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics