A tiny rich-text supporting library for iOS.
- High performance text layout and rendering.
- Attachments embedding with native view supports.
- Clickable links supports.
- Custom draw callbacks.
- Auto layout integration (experimental).
- macOS (11.0+)
- iOS (13.0+)
Add Litext as a dependency in your Package.swift file:
dependencies: [ .package(url: "https://github.com/Helixform/Litext.git", branch: "main") ]
import Litext let label = LTXLabel() view.addSubview(label) // Create attributed string with styling. let attributedString = NSMutableAttributedString( string: "Hello, Litext!", attributes: [ .font: NSFont.systemFont(ofSize: 16), .foregroundColor: NSColor.labelColor ] ) // Set the attributed text to display. label.attributedText = attributedString
let linkString = NSAttributedString( string: "Visit our website", attributes: [ .font: NSFont.systemFont(ofSize: 14), .link: URL(string: "https://example.com")!, .foregroundColor: NSColor.linkColor ] ) attributedString.append(linkString) // Handle link taps. label.tapHandler = { highlightRegion in if let url = highlightRegion.attributes[.link] as? URL { NSWorkspace.shared.open(url) } }
// Create and configure attachment. let attachment = LTXAttachment() let switchView = NSSwitch() attachment.view = switchView attachment.size = switchView.intrinsicContentSize // Add attachment to text. // // `kCTRunDelegateAttributeName` must be included to ensure the // attachment is rendered correctly. attributedString.append( NSAttributedString( string: LTXReplacementText, attributes: [ .LTXAttachmentAttributeName: attachment, kCTRunDelegateAttributeName as NSAttributedString.Key: attachment.runDelegate ] ) )
Licensed under MIT License, see LICENSE for more information.