GithubHelp home page GithubHelp logo

rmkitty / attributedstringbuilder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stevebarnegren/attributedstringbuilder

0.0 1.0 0.0 506 KB

Easily construct attributed strings in swift

License: MIT License

Ruby 5.01% Swift 94.99%

attributedstringbuilder's Introduction

AttributedStringBuilder

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

Usage

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 = [
    .textColor(UIColor.black),
    .font( UIFont.systemFont(ofSize: 16, weight: UIFontWeightRegular) ),
    .alignment(.center),
]

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

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 = [
    .alignment(.center),
    .textColor(UIColor.orange),
    .font( UIFont(name: "AvenirNext-Bold", size: 30)! )
]

builder
    .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)!)])

builder.attributedString

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)!),
    .textColor(UIColor.white),
    .strokeColor(UIColor.magenta),
    .strokeWidth(-8),
    .kerning(5)
]

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

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

let awesomeAttributes: [AttributedStringBuilder.Attribute] = [
    .font(UIFont(name: "AvenirNext-Bold", size: 40)!),
    .textColor(UIColor.yellow),
    .kerning(5),
    .shadow(shadow),
    .skew(0.3),
    .underline(true)
]

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

builder.attributedString

Shadow Example

Images

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 = [
    .font(font),
    .underline(true),
    .textColor(UIColor.purple)
]
        
let image = UIImage(named: "PurpleMonster")!
        
builder
    .image(image, withSizeFittingFontUppercase: font)
    .text("Hello")
    .image(image, withSizeFittingFontLowercase: font)

Images Example

Installation

CocoaPods

Add the following line to your Podfile:

pod "AttributedStringBuilder"
Manual

Copy AttributedStringBuilder.swift in to your project

Who?

@SteveBarnegren

License

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

attributedstringbuilder's People

Contributors

evgeniymikholap avatar stevebarnegren avatar

Watchers

 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.