Coder Social home page Coder Social logo

wooky83 / nsattributedstringbuilder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ethanhuang13/nsattributedstringbuilder

0.0 1.0 0.0 5.34 MB

Composing NSAttributedString with SwiftUI-style syntax

License: MIT License

Swift 96.79% Ruby 3.21%

nsattributedstringbuilder's Introduction

NSAttributedStringBuilder

Build Status codecov GitHub release GitHub top language License Twitter Donate

Composing NSAttributedString with SwiftUI-style syntax, powered by Result Builder.

Project Link: https://github.com/ethanhuang13/NSAttributedStringBuilder

Features

Features
๐Ÿฆ Open source library written in Swift 5.4
๐Ÿฌ SwiftUI-like syntax
๐Ÿ’ช Support most attributes in NSAttributedString.Key
๐Ÿ“ฆ Distribution with Swift Package Manager
๐Ÿงช Fully tested code
๐Ÿ›  Continuously integrates in Swift Source Compatibility Suite

How to use?

Traditionally we compose a NSAttributedString like this:

let mas = NSMutableAttributedString(string: "")
mas.append(NSAttributedString(string: "Hello world", attributes: [.font: UIFont.systemFont(ofSize: 24), .foregroundColor: UIColor.red]))
mas.append(NSAttributedString(string: "\n"))
mas.append(NSAttributedString(string: "with Swift", attributes: [.font: UIFont.systemFont(ofSize: 20), .foregroundColor: UIColor.orange]))

Now, with NSAttributedStringBuilder, we can use SwiftUI-like syntax to declare NSAttributedString:

let attributedString = NSAttributedString {
    AText("Hello world")
        .font(.systemFont(ofSize: 24))
        .foregroundColor(.red)
    LineBreak()
    AText("with Swift")
        .font(.systemFont(ofSize: 20))
        .foregroundColor(.orange)
}

Requirements

Xcode 12.5. This project uses Swift 5.4 feature Result Builder.

Installation

Swift Package

Open your project in Xcode 12, navigate to Menu -> Swift Packages -> Add Package Dependency and enter https://github.com/ethanhuang13/NSAttributedStringBuilder to install.

CocoaPods

Add pod 'NSAttributedStringBuilder13' to your Podfile.

SwiftUI Sample Project

Besides clearer NSAttributedString syntax, since NSAttributedStringBuilder uses Result Builder it also enables API to build components in UIViewRepresentable(which embeds UIView in a SwiftUI View).

Just like a SwiftUI Text takes a String as input, the purpose of AttributedText in the sample project is to take a NSAttributedString as input and render in SwiftUI.

To achieve this, AttributedText.swift uses @NSAttributedStringBuilder to support SwiftUI-style syntax:

AttributedText.swift

Then using an AttributedText will be like: ContentView.swift

Open the sample in /SwiftUISampleApp/AttributedTextSample.xcodeproj and check AttributedText. It uses UITextView, you can also use UILabel or NSTextView.

TODO

  • Better tests for image attachment

Known Issue

  • NSAttributedString does not support link color, therefore Link component with a .color() modifier has no effect. Alternatively you need to specify in UITextView.linkTextAttributes or .tintColor.

Others

Initially discussed on this Twitter thread. Some code are inspired by zonble๐Ÿ™.

nsattributedstringbuilder's People

Contributors

ethanhuang13 avatar

Watchers

James Cloos 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.