All Projects → SteveBarnegren → AttributedStringBuilder

SteveBarnegren / AttributedStringBuilder

Licence: MIT license
Easily construct attributed strings in swift

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to AttributedStringBuilder

Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (+326.09%)
Mutual labels:  nsattributedstring
Gaudi
A swift theming framework for UIKit for easy adoption of multi theme and iOS dark mode
Stars: ✭ 36 (-47.83%)
Mutual labels:  nsattributedstring
Swiftrichstring
👩‍🎨 Elegant Attributed String composition in Swift sauce
Stars: ✭ 2,744 (+3876.81%)
Mutual labels:  nsattributedstring
Nerdyui
An easy way to create and layout UI components for iOS.
Stars: ✭ 381 (+452.17%)
Mutual labels:  nsattributedstring
Nsattributedstringbuilder
Composing NSAttributedString with SwiftUI-style syntax
Stars: ✭ 660 (+856.52%)
Mutual labels:  nsattributedstring
Styledtextkit
Declarative building and fast rendering attributed string library.
Stars: ✭ 1,171 (+1597.1%)
Mutual labels:  nsattributedstring
Cupcake
An easy way to create and layout UI components for iOS (Swift version).
Stars: ✭ 273 (+295.65%)
Mutual labels:  nsattributedstring
TypedTextAttributes
🖍 The Library Creating Text Attributes with Type-Safety
Stars: ✭ 55 (-20.29%)
Mutual labels:  nsattributedstring
Atributika
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
Stars: ✭ 845 (+1124.64%)
Mutual labels:  nsattributedstring
Bostring
Create NSAttributedString like a boss!
Stars: ✭ 193 (+179.71%)
Mutual labels:  nsattributedstring
Commonmarkattributedstring
Create NSAttributedStrings from Markdown Text
Stars: ✭ 382 (+453.62%)
Mutual labels:  nsattributedstring
Typeset
Deal with AttributedString efficiently
Stars: ✭ 458 (+563.77%)
Mutual labels:  nsattributedstring
Swiftyattributes
A Swifty API for attributed strings
Stars: ✭ 1,303 (+1788.41%)
Mutual labels:  nsattributedstring
Sjattributesfactory
Simplify operation NSAttributedString, make writing easier. Attributes String Editing Factory. iOS 富文本编辑, 让代码更清晰. 文本编辑, 高度计算, 正则匹配等待... 简便操作, 让你爽到爆!
Stars: ✭ 372 (+439.13%)
Mutual labels:  nsattributedstring
AttributedStringWrapper
a simple packaging for NSAttributedString to make the developers easy to use
Stars: ✭ 16 (-76.81%)
Mutual labels:  nsattributedstring
Transformer
Easy Attributed String Creator
Stars: ✭ 278 (+302.9%)
Mutual labels:  nsattributedstring
Allkit
🛠 Async List Layout Kit
Stars: ✭ 40 (-42.03%)
Mutual labels:  nsattributedstring
RZColorful
NSAttributedString富文本的方法集合,以及简单优雅的使用其多种属性
Stars: ✭ 53 (-23.19%)
Mutual labels:  nsattributedstring
StringStylizer
Type strict builder class for NSAttributedString.
Stars: ✭ 75 (+8.7%)
Mutual labels:  nsattributedstring
Cjlabel
A drop-in replacement for UILabel that supports NSAttributedString, rich text, display any view, links, select copy and more
Stars: ✭ 140 (+102.9%)
Mutual labels:  nsattributedstring

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].