All Projects → ethanhuang13 → Nsattributedstringbuilder

ethanhuang13 / Nsattributedstringbuilder

Licence: mit
Composing NSAttributedString with SwiftUI-style syntax

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Nsattributedstringbuilder

Swiftyattributes
A Swifty API for attributed strings
Stars: ✭ 1,303 (+97.42%)
Mutual labels:  nsattributedstring
AttributedStringBuilder
Easily construct attributed strings in swift
Stars: ✭ 69 (-89.55%)
Mutual labels:  nsattributedstring
Sjattributesfactory
Simplify operation NSAttributedString, make writing easier. Attributes String Editing Factory. iOS 富文本编辑, 让代码更清晰. 文本编辑, 高度计算, 正则匹配等待... 简便操作, 让你爽到爆!
Stars: ✭ 372 (-43.64%)
Mutual labels:  nsattributedstring
Bostring
Create NSAttributedString like a boss!
Stars: ✭ 193 (-70.76%)
Mutual labels:  nsattributedstring
TypedTextAttributes
🖍 The Library Creating Text Attributes with Type-Safety
Stars: ✭ 55 (-91.67%)
Mutual labels:  nsattributedstring
CuteAttribute
An elegant way to deal with attributed string in swift.
Stars: ✭ 65 (-90.15%)
Mutual labels:  nsattributedstring
Allkit
🛠 Async List Layout Kit
Stars: ✭ 40 (-93.94%)
Mutual labels:  nsattributedstring
Nudein
An easy-to-use attributed text view for iOS Apps,use like masonry
Stars: ✭ 450 (-31.82%)
Mutual labels:  nsattributedstring
RZColorful
NSAttributedString富文本的方法集合,以及简单优雅的使用其多种属性
Stars: ✭ 53 (-91.97%)
Mutual labels:  nsattributedstring
Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (-55.45%)
Mutual labels:  nsattributedstring
Swiftrichstring
👩‍🎨 Elegant Attributed String composition in Swift sauce
Stars: ✭ 2,744 (+315.76%)
Mutual labels:  nsattributedstring
StringStylizer
Type strict builder class for NSAttributedString.
Stars: ✭ 75 (-88.64%)
Mutual labels:  nsattributedstring
Cupcake
An easy way to create and layout UI components for iOS (Swift version).
Stars: ✭ 273 (-58.64%)
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 (-78.79%)
Mutual labels:  nsattributedstring
Nerdyui
An easy way to create and layout UI components for iOS.
Stars: ✭ 381 (-42.27%)
Mutual labels:  nsattributedstring
Styledtextkit
Declarative building and fast rendering attributed string library.
Stars: ✭ 1,171 (+77.42%)
Mutual labels:  nsattributedstring
Prestyler
Elegant text formatting tool in Swift 🔥
Stars: ✭ 36 (-94.55%)
Mutual labels:  nsattributedstring
Typeset
Deal with AttributedString efficiently
Stars: ✭ 458 (-30.61%)
Mutual labels:  nsattributedstring
Commonmarkattributedstring
Create NSAttributedStrings from Markdown Text
Stars: ✭ 382 (-42.12%)
Mutual labels:  nsattributedstring
Transformer
Easy Attributed String Creator
Stars: ✭ 278 (-57.88%)
Mutual labels:  nsattributedstring

NSAttributedStringBuilder

Build Status codecov GitHub release GitHub top language License Twitter Donate

Composing NSAttributedString with SwiftUI-style syntax, powerd by the now pitching Function Builder.

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

Features

Features
🐦 Open source library written in Swift 5.1
🍬 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 11. This project uses Swift 5.1 feature Function Builder.

Installation

Swift Package

Open your project in Xcode 11, 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 Function Builder it also enables API to build components in UIViewRepresentable(which embbed 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🙏.

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].