Swiftpack.co - Package - jiachenyee/AttributedString

AttributedString

A simple way to deal with NSAttributedStrings

Demo

Demo Text Snippet
// lyrics is just a String of the lyrics to Never Gonna Give You Up
let rickroll = lyrics

    // Converts the lyrics (String) to Attributed
    .attributed() 

    // Highlights "Never gonna" blue
    .highlight(usingRegex: "Never gonna", options: [], color: .systemBlue) 

    // Sets text color of "Never gonna" to system background
    .textColor(usingRegex: "Never gonna", options: [], color: .systemBackground) 

    // Sets anything within 2 brackets as italics 
    // E.g. (Hello) or (123)
    .italic(usingRegex: "\\((.)+\\)", options: [])

    // Makes everything between 0 and 23 bold
    // Basically the title
    .bold(in: 0...23)

// Set attributed text of a textView to the rickroll string generated
textView.attributedText = rickroll.attributedString
Demo Table Snippet
let table = [["never", "gonna", "give", "you", "up"],
             ["never", "gonna", "let", "you", "down"],
             ["never", "gonna", "run", "around", "and", "desert", "you"],
             ["never", "gonna", "make", "you", "cry"],
             ["never", "gonna", "say", "goodbye"],
             ["never", "gonna", "tell", "a", "lie", "and", "hurt", "you"]]```
             
let attributed = try! Attributed(table: table],

                                 // Removing header rows, default is true
                                 hasHeaderRow: false, 

                                 // Turning on table borders, default is true
                                 hasBorders: true,
                   
                                 // Setting border color to blue, default is UIColor.label
                                 color: .systemBlue)

Features

  • ☑ Easily set text as bold, italics or both at the same time.
  • ☑ Set text colors and highlight colors using ranges
  • ☑ Use Regular Expressions to easily provide attributes to certain parts of a String
  • ☑ Easily underline words
  • ☑ Easily embed an image into the text
  • ☑ Easily create tables using 2D String arrays

To-Dos

  • ☐ Text alignment support

Installation

Swift Package Manager

let package = Package(
    name: "MyApp",
    dependencies: [
        .Package(url: "https://github.com/jiachenyee/AttributedString.git", majorVersion: 1)
    ]
)

Usage

Result: Never gonna give you up

let attributes = "Never gonna give you up"
    .attributed()
    .bold()
    .italic(in: 0...5)

Creating an Attributed String

Creating using String
Attributed("Never gonna give you up")
"Never gonna give you up".attributed()
Creating using an NSAttributedString
let nsAttributedString = NSMutableAttributedString(string: "Never gonna let you down")

Attributed(nsAttributedString)
nsAttributedString.attributed()
Creating using UIImage
Attributed(UIImage(named: "Here's an image")!)

UIImage(named: "image").attributed()
Creating using HTML/RTF/TXT files
Attributed(usingData: htmlData, ofType: .html)

Attributed(usingData: rtfData, ofType: .rtf)

Attributed(usingData: htmlData, ofType: .html, characterEncoding: .utf16)

Concatenating or Joining

Concatenating multiple Strings together to create a String
Attributed("Never gonna give you up", "Never gonna let you down", ...)

Attributed(["Never gonna give you up", "Never gonna let you down"])
Concatenating multiple NSAttributedStrings together to create an AttributedString
Attributed(nsAttributedString, nsAttributedString, ...)

Attributed([nsAttributedString, nsAttributedString])
Concatenating multiple AttributedStrings together to create a larger AttributedString
Attributed("Never gonna give you up") + Attributed("Never gonna let you down")

Attributed(.init("Never gonna give you up"), .init("Never gonna let you down"), ...)

Attributed([.init("Never gonna give you up"), .init("Never gonna let you down")])

Converting from AttributedString

to String
.attributedString.string
to NSAttributedString

Important to set it as the text in a Label or TextView

.attributedString

Adding Styles

Bold

Make the whole string bold

Result: Never gonna give you up

.bold()
Make a range of text bold

Result: Never gonna give you up

.bold(in: 0...5)
Make a range of text bold using NSRange

Result: Never gonna give you up

.bold(in: NSRange(location: 0, length: 5))
Make a range of text bold using Regular Expressions

Result: 1234ABC

.bold(usingRegex: "[0-9]", options: [])

Italics

Make the whole string italic

Result: Never gonna give you up

.italic()
Make a range of text italic

Result: Never gonna give you up

.italic(in: 0...5)
Make a range of text italic using NSRange

Result: Never gonna give you up

.italic(in: NSRange(location: 0, length: 5))
Make a range of text italic using Regular Expressions

Result: 1234ABC

.italic(usingRegex: "[0-9]", options: [])

Underline

Make the whole string underlined
.underline()
Make a range of text underlined
.underline(in: 0...5)
Make a range of text underlined using NSRange
.underline(in: NSRange(location: 0, length: 5))
Make a range of text red underlined
.underline(in: 0...5, color: .systemRed)
Make a range of text red with single underlined style
.underline(in: 0...5, color: .systemRed, style: .single)
Make a range of text underlined using Regular Expressions
.underline(usingRegex: "[0-9]", options: [], color: .systemRed, style: .single)

Text Colors

Set a text color for the whole string
.textColor(color: .systemRed)
Set a text color for the specific range
.textColor(in: 0...5, color: .systemRed)
Set a text color for the specific range using NSRange
.textColor(in: NSRange(location: 0, length: 5), color: .systemRed)
Set a text color based on Regular Expression
.textColor(usingRegex: "[0-9]", options: [], color: .systemRed)

Highlight Colors

Set a highlight color for the whole string
.highlight(with: .systemRed)
Set a highlight color within a specific range
.highlight(in: 0...5, with: .systemRed)
Set a highlight color within a specific range using NSRange
.highlight(in: NSRange(location: 0, length: 5), with: .systemRed)
Set a highlight color based on Regular Expression
.highlight(usingRegex: "[0-9]", options: [], color: .systemRed)

Custom Fonts

Set a custom font for the whole string

Result: Never gonna give you up

.font(with: UIFont.monospacedSystemFont(ofSize: 20, weight: .regular))
Set a custom font within a specific range

Result: Never gonna give you up

.font(in: 0...5, with: UIFont.monospacedSystemFont(ofSize: 20, weight: .regular))
Set a custom font within a specific range using NSRange

Result: Never gonna give you up

.font(in: NSRange(location: 0, length: 5), with: UIFont.monospacedSystemFont(ofSize: 20, weight: .regular))
Set a custom font based on Regular Expression

Result: 1234ABCD9EF

.font(usingRegex: "[0-9]", options: [], selectedFont: UIFont.monospacedSystemFont(ofSize: 20, weight: .regular))

Removing Styles

Bold

Remove bold from the whole string
.removeBold()
Remove bold for a range of text
.removeBold(in: 0...5)
Remove bold from a range of text using NSRange
.removeBold(in: NSRange(location: 0, length: 5))
Remove bold using regular expressions
.bold(usingRegex: "[0-9]", options: [])

Italics

Remove italic from the whole string
.removeBold()
Remove italic for a range of text
.removeItalic(in: 0...5)
Remove italic for a range of text using NSRange
.removeItalic(in: NSRange(location: 0, length: 5))
Remove italic using Regular Expressions
.removeItalic(usingRegex: "[0-9]", options: [])

Underline

Remove underline from the whole text
.removeUnderline()
Remove underline within a range
.removeUnderline(in: 0...5)
Remove underline within a range using NSRange
.removeUnderline(in: NSRange(location: 0, length: 5))
Remove underline using Regular Expressions
.removeUnderline(usingRegex: "[0-9]", options: [], color: .systemRed, style: .single)

Creating Tables

Creating a simple table using 2D String array

Github

link
Stars: 3

Dependencies

Used By

Total: 0

Releases

1.0.5 - 2020-09-11 17:47:32

Updated with concatenating

1.0.4 - 2020-09-11 17:12:03

Minor bug fixes

1.0.3 - 2020-09-11 17:02:13

yeah 1.0.2 was broken

1.0.2 - 2020-09-11 16:59:39

  • Fixed access level bug

1.0.1 - 2020-09-11 16:50:46

Added tables support

1.0.0 - 2020-09-09 12:14:01

simple NSAttributedString replacement