GitHawkApp/StyledTextKit
소개
어트리뷰티드 문자열을 선언적으로 구성하고 빠르게 렌더링하는 라이브러리입니다.
디자인 및 미디어What you can do
- 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)
측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- -1
- in 1 month
- -5
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Original
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 orStrings while re-using the string's current attributes, saving you from repetitive.fontand.foregroundColorstyling. - 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()andrestore()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
StyledTextobjects - Call
build()when finished to generate aStyledTextStringobject - Call
render(contentSizeCategory:)to create anNSAttributedString
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:
- Get the current width and
UIContentSizeCategory - Go to a background queue
- Build text
- Warm caches
- Return to the main queue
- 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
- @ocrickard who built ComponentTextKit and taught me the basics.
- Created with ❤️ by Ryan Nystrom
관련 프로젝트 살펴보기
불러오는 중…