All Projects → GitHawkApp → Styledtextkit

GitHawkApp / Styledtextkit

Licence: mit
Declarative building and fast rendering attributed string library.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Styledtextkit

Swiftrichstring
👩‍🎨 Elegant Attributed String composition in Swift sauce
Stars: ✭ 2,744 (+134.33%)
Mutual labels:  text, nsattributedstring
Prestyler
Elegant text formatting tool in Swift 🔥
Stars: ✭ 36 (-96.93%)
Mutual labels:  text, nsattributedstring
Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (-74.89%)
Mutual labels:  text, nsattributedstring
Urlify
A simple macOS app to create valid file and url names from clipboard text.
Stars: ✭ 44 (-96.24%)
Mutual labels:  text
Mybox
Easy tools of document, image, file, network, location, color, and media.
Stars: ✭ 45 (-96.16%)
Mutual labels:  text
Input Mask Android
User input masking library repo.
Stars: ✭ 1,060 (-9.48%)
Mutual labels:  text
Automator
Various Automator and AppleScript workflow and scripts for simplifying life
Stars: ✭ 68 (-94.19%)
Mutual labels:  text
Allkit
🛠 Async List Layout Kit
Stars: ✭ 40 (-96.58%)
Mutual labels:  nsattributedstring
Moondweller
A text-based adventure. You will definitely die.
Stars: ✭ 60 (-94.88%)
Mutual labels:  text
React Contenteditable
React component for a div with editable contents
Stars: ✭ 1,057 (-9.74%)
Mutual labels:  text
Sketch Textbox Fit Content
Set the height of a selected text layer or all text layers in a selected group to it's content's height.
Stars: ✭ 49 (-95.82%)
Mutual labels:  text
Art
🎨 ASCII art library for Python
Stars: ✭ 1,026 (-12.38%)
Mutual labels:  text
Ngram
Fast n-Gram Tokenization
Stars: ✭ 55 (-95.3%)
Mutual labels:  text
Randomdatagenerator
This is a configurable generator to create random data like Lorum Ipsum Text, Words, Text Patterns, First/Last Names, MAC-Addresses, IP-Addresses, Guids and DateTime.
Stars: ✭ 45 (-96.16%)
Mutual labels:  text
Social Text View
A custom Android TextView that highlights social media lingo (#hashtags, @mentions, phone, emails, and urls).
Stars: ✭ 64 (-94.53%)
Mutual labels:  text
Gpt2 Telegram Chatbot
GPT-2 Telegram Chat bot
Stars: ✭ 41 (-96.5%)
Mutual labels:  text
Rumble
⛈️ Rumble 1.11.0 "Banyan Tree"🌳 for Apache Spark | Run queries on your large-scale, messy JSON-like data (JSON, text, CSV, Parquet, ROOT, AVRO, SVM...) | No install required (just a jar to download) | Declarative Machine Learning and more
Stars: ✭ 58 (-95.05%)
Mutual labels:  text
Insert Text At Cursor
Fast crossbrowser insertion of text at cursor position in a textarea / input
Stars: ✭ 49 (-95.82%)
Mutual labels:  text
Ybattributetexttapforswfit
一行代码添加文本点击事件(swfit4.2版本)/a fast way to implement click event text(for swfit4.2)
Stars: ✭ 47 (-95.99%)
Mutual labels:  text
Calyx
A Ruby library for generating text with recursive template grammars.
Stars: ✭ 51 (-95.64%)
Mutual labels:  text

StyledTextKit is a declarative attributed string library for fast rendering and easy string building. It serves as a simple replacement to NSAttributedString and UILabel for background-thread sizing and bitmap caching.

Features

  • Declarative attributed string building API
  • Find text sizes on a background thread without sanitizer warnings
  • Cache rendered text bitmaps for improved performance
  • Custom attribute interaction handling (link taps, etc)

Installation

Just add StyledTextKit to your Podfile and install. Done!

pod 'StyledTextKit'

Usage

Building NSAttributedStrings

StyledTextKit lets you build complex NSAttributedStrings:

  • Append NSAttributedStrings or Strings while re-using the string's current attributes, saving you from repetitive .font and .foregroundColor styling.
  • Intermix complex font traits like bold and italics to get bold italics.
  • Handle dynamic text size at string render time. Lets you build the string once and re-render it on device text-size changes.
  • Call save() and restore() to push/pop style settings, letting you build complex text styles without complex code.
let attributedString = StyledTextBuilder(text: "Foo ")
  .save()
  .add(text: "bar", traits: [.traitBold])
  .restore()
  .add(text: " baz!")
  .build()
  .render(contentSizeCategory: .large)

Foo bar baz!

The basic steps are:

  • Create a StyledTextBuilder
  • Add StyledText objects
  • Call build() when finished to generate a StyledTextString object
  • Call render(contentSizeCategory:) to create an NSAttributedString

Rendering Text Bitmaps

Create a StyledTextRenderer for sizing and rendering text by initializing it with a StyledTextString and a UIContentSizeCategory.

let renderer = StyledTextRenderer(
  string: string,
  contentSizeCategory: .large
)

Once created, you can easily get the size of the text constrained to a width:

let size = renderer.size(in: 320)

You can also get a bitmap of the text:

let result = renderer.render(for: 320)
view.layer.contents = result.image

StyledTextView

To make rendering and layout of text in a UIView simpler, use StyledTextView to manage display as well as interactions. All you need is a StyledTextRenderer and a width and you're set!

let view = StyledTextView()
view.configure(with: renderer, width: 320)

Set a delegate on the view to handle tap and long presses:

view.delegate = self

// StyledTextViewDelegate
func didTap(view: StyledTextView, attributes: [NSAttributedStringKey: Any], point: CGPoint) {
  guard let link = attributes[.link] else { return }
  show(SFSafariViewController(url: link))
}

Background Rendering

StyledTextKit exists to do background sizing and rendering of text content so that scrolling large amounts of text is buttery smooth. The typical pipeline to do this is:

  1. Get the current width and UIContentSizeCategory
  2. Go to a background queue
  3. Build text
  4. Warm caches
  5. Return to the main queue
  6. Configure your views
// ViewController.swift

let width = view.bounds.width
let contentSizeCategory = UIApplication.shared.preferredContentSizeCategory

DispatchQueue.global().async {
  let builder = StyledTextBuilder(...)
  let renderer = StyledTextRenderer(string: builder.build(), contentSizeCategory: contentSizeCategory)
    .warm(width: width) // warms the size cache

  DispatchQueue.main.async {
    self.textView.configure(with: renderer, width: width)
  }
}

FAQ

Why not use UITextView?

Prior to iOS 7, UITextView just used WebKit under the hood and was terribly slow. Now that it uses TextKit, it's significantly faster but still requires all sizing and rendering be done on the main thread.

For apps with lots of text embedded in UITableViewCells or UICollectionViewCells, UITextView bring scrolling to a grinding halt.

Acknowledgements

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