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.

Overview

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.

Issues
  • Found a new use case that is not possible with current implementation

    Found a new use case that is not possible with current implementation

    I need to attach some kind of meaning to a part of a text, for example: Don't have an account? *Sign Up*. When user taps on a text I'll determine what character index was tapped and get an attribute that I'm interested in using: func attribute(_ attrName: NSAttributedString.Key, at location: Int, effectiveRange range: NSRangePointer?) -> Any?. Currently, I can determine that Sign Up part has a bold font and know that the user tapped there but it's not a clear solution.

    New API proposal (no DownSwiftConfig anymore):

    let downSwift = DownSwift()
    downSwift.register(tag: "*", attributes: [NSAttributedString.Key.font: boldFont, NSAttributedString.Key.link: "sign_up"])
    let attributedText = downSwift.parse("Don't have an account? *Sign Up*")
    

    Alternative is to support markdown links (DownSwiftConfig should have linkFont, linkColor, linkUnderline):

    let downSwift = DownSwift()
    let attributedText = downSwift.parse("Don't have an account? [Sign Up](sign_up)")
    

    The solution based on the first with support for markdown links (this I like the most):

    let downSwift = DownSwift()
    downSwift.register(tag: "*", attributes: [NSAttributedString.Key.font: boldFont, NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single])
    let attributedText = downSwift.parse("Don't have an account? *[Sign Up](sign_up)*")
    
    opened by zummenix 1
  • New parsing logic

    New parsing logic

    opened by UsiantsevStepanSaritasa 1
  • Put framework's files together

    Put framework's files together

    opened by UsiantsevStepanSaritasa 0
  • Create LICENSE

    Create LICENSE

    opened by UsiantsevStepanSaritasa 0
  • Added .podspec

    Added .podspec

    opened by UsiantsevStepanSaritasa 0
  • Fix access levels of config and main class

    Fix access levels of config and main class

    opened by UsiantsevStepanSaritasa 0
  • Improved algorithm of a string tokenizing

    Improved algorithm of a string tokenizing

    Added opportunity of customisation symbols that define text style area.

    opened by UsiantsevStepanSaritasa 0
  • Change if else to switch in DownSwift

    Change if else to switch in DownSwift

    opened by UsiantsevStepanSaritasa 0
  • Add symbol skip. Remove TextStyle customization

    Add symbol skip. Remove TextStyle customization

    opened by UsiantsevStepanSaritasa 0
  • Renamed config entity. Minor refactoring

    Renamed config entity. Minor refactoring

    opened by UsiantsevStepanSaritasa 0
Releases(0.0.8)
Owner
Stepan Usiantsev
Stepan Usiantsev
🌍⏩📄 Convert ISO8859 1-16 Encoded Text to String in Swift. Supports iOS, tvOS, watchOS and macOS.

ISO8859 Convert ISO8859 1-16 Encoded Text to String in Swift. Usage let encoding = ISO8859.part1 let string = String([...], iso8859Encoding: encoding)

Devran Cosmo Uenal 17 Nov 12, 2020
Swift String Validator. Simple lib for ios to validate string and UITextFields text for some criterias

Swift String validator About Library for easy and fastest string validation based on сciterias. Instalation KKStringValidator is available through Coc

Kostya 17 Dec 20, 2019
👩‍🎨 Elegant Attributed String composition in Swift sauce

Elegant Attributed String composition in Swift sauce SwiftRichString is a lightweight library which allows to create and manipulate attributed strings

Daniele Margutti 2.7k Nov 10, 2021
Easy string decoration with styles

StyleDecorator Design string simply by linking attributes. Example Create Decorator with specific Style and link it at the end of needed string or wra

Dmytro Pylypenko 15 Nov 4, 2021
Beautiful, easy attributed strings in Swift

BonMot (pronounced Bon Mo, French for good word) is a Swift attributed string library. It abstracts away the complexities of the iOS, macOS, tvOS, and

Rightpoint 3.2k Nov 22, 2021
Easiest way to create an attributed UITextView (with support for multiple links and from html)

AttributedTextView Easiest way to create an attributed UITextView (with support for multiple links and html). See the demo app and the playground for

Edwin Vermeer 422 Nov 26, 2021
An easier way to compose attributed strings

TextAttributes makes it easy to compose attributed strings. let attrs = TextAttributes() .font(name: "HelveticaNeue", size: 16) .foregroundCol

Damien 2.2k Nov 25, 2021
A Swifty API for attributed strings

SwiftyAttributes A Swifty API for attributed strings. With SwiftyAttributes, you can create attributed strings like so: let fancyString = "Hello World

Eddie Kaiger 1.4k Nov 16, 2021
µframework for Attributed strings.

Attributed µframework for Attributed strings. What is Attributed? Attributed aims to be a drop in replacement to the current version of the NSAttribut

Nicholas Maccharoli 746 Nov 13, 2021
Format iOS attributed strings easily

Texstyle allows you to format attributed strings easily. Features Applying attributes with strong typing and autocompletion Cache for attributes Subst

Rosberry 72 Jun 3, 2021
Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform.

twitter-text This repository is a collection of libraries and conformance tests to standardize parsing of Tweet text. It synchronizes development, tes

Twitter 2.6k Nov 22, 2021
Powerful text framework for iOS to display and edit rich text.

YYText Powerful text framework for iOS to display and edit rich text. (It's a component of YYKit) Features UILabel and UITextView API compatible High

null 8.7k Nov 17, 2021
Lightweight library to set an Image as text background. Written in swift.

![](https://img.shields.io/badge/Swift 2-compatible-4BC51D.svg?style=flat-square) Simple and light weight UIView that animate text with an image. Demo

Lucas Ortis 555 Nov 12, 2021
A category on NSString to convert Emoji Cheat Sheet codes to their equivalent Unicode characters

NSString+Emojize A category on NSString to turn codes from Emoji Cheat Sheet into Unicode emoji characters. Getting Started In order to use NSString+E

DIY.org 642 Oct 2, 2021
A comprehensive, lightweight string extension for Swift

SwiftString SwiftString is a lightweight string extension for Swift. This library was motivated by having to search StackOverflow for common string op

Andrew Mayne 1.6k Nov 26, 2021
RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.

RichEditorView RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing. Written in Swift 4 Supports iOS 8+ through Cocoapod

Caesar Wirth 1.8k Nov 24, 2021
Converts Markdown files and strings into NSAttributedStrings with lots of customisation options.

SwiftyMarkdown 1.0 SwiftyMarkdown converts Markdown files and strings into NSAttributedStrings using sensible defaults and a Swift-style syntax. It us

Simon Fairbairn 1.4k Nov 19, 2021
A Cross-Platform String and Regular Expression Library written in Swift.

Guitar ?? A Cross-Platform String and Regular Expression Library written in Swift. About This library seeks to add common string manipulation function

Arthur Ariel Sabintsev 654 Oct 28, 2021
Swift emoji string parsing library

Croc is a library for parsing emojis on iOS. It provides a simple and lightweight interface for detecting, generating, categorizing and managing emoji

Joe Kalash 125 Sep 27, 2021