Swiftpack.co - UsiantsevStepanSaritasa/DownSwift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by UsiantsevStepanSaritasa.
UsiantsevStepanSaritasa/DownSwift 0.0.8
Simple and lightweight Swift library that helps to define text style areas in given string with your own configuration, apply those styles and convert string into attributed string.
⭐️ 3
🕓 2 years ago
iOS
.package(url: "https://github.com/UsiantsevStepanSaritasa/DownSwift.git", from: "0.0.8")

DownSwift

Build Status MIT licensed codebeat badge

Simple and lightweight Swift library that helps to define text style areas in given string with your own configuration, apply those styles and convert string into attributed string.

Installation

Using CocoaPods:

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
    pod 'DownSwift'
end

Replace YOUR_TARGET_NAME and then, in the Podfile directory, type:

$ pod install

Be careful with pod's version! Sometimes CocoaPods can miss the latest version within default installation so there are solutions:

  1. Run pod update in terminal within Podfile directory after pod's installation;

OR

  1. Strictly define pod's version in Podfile:
# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
# Where '0.0.x' is a pod's version
    pod 'DownSwift', '~> 0.0.x'
end

Using Swift Package Manager:

To add DownSwift to your project, select File → Swift Packages → Add Package Dependency and enter the GitHub URL for DownSwift.

Check Adding Package Dependencies to Your App for detailed instructions.

How to use

First of all, import library and initialize main class:

import DownSwift

class YourBeautifulClass {
    ...
    let downSwift = DownSwift()
    ...
}

Then create tags that will define text style areas in string:

    // EXAMPLE
    // For custom fonts I've used "Oswald-Regular" and "Oswald-Bold" fonts.
    ...
    // `*` tag will define bold style area
    downSwift.register(tag: "*", attributes: [
        NSAttributedString.Key.font: customBoldFont,
        NSAttributedString.Key.foregroundColor: UIColor.systemBlue
    ])
    // `~` tag will define italic style area
    downSwift.register(tag: "~", attributes: [
        NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 17),
        NSAttributedString.Key.foregroundColor: UIColor.systemRed
    ])
    // `|` tag will define strikethrough style area
    downSwift.register(tag: "|", attributes: [
        NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
        NSAttributedString.Key.strikethroughColor: UIColor.gray
    ])
    // To customize regular text style use `nil` in `tag` parameter!
    downSwift.register(tag: nil, attributes: [NSAttributedString.Key.font: customRegularFont])
    ...

If you want to skip any characters in your string then use \\.

After you created needed tags you can parse your string into attributed string with given style areas:

    let string = "Hello and *welcome* to the ~example area!~ We are |sad| ~happy\\*~ that you are *here*!!!"
    
    textLabel.attributedText = downSwift.parse(string)

Output:

img

License

DownSwift is available under the MIT license. Check LICENSE file for more info.

GitHub

link
Stars: 3
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.0.8. Customisation redesign.
2 years ago

Customisation logic has been changed so now it's easier for you to design your own style areas. Also library became almost weightless.

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