AttributedText
AttributedText is a SwiftUI component that renders an AttributedString with an appearance close to SwiftUI's Text. It is built on top of UITextView, so it also supports rich text features such as links, text selection, and NSTextAttachment.
Requirements
- iOS 17.0+
- Swift 6.2+
- Xcode 26+
Installation
Add the following URL to your project using Swift Package Manager:
https://github.com/noppefoxwolf/AttributedText.git
Usage
import AttributedText
import SwiftUI
import UIKit
struct ContentView: View {
var body: some View {
AttributedText(message)
.lineLimit(3)
}
private var message: AttributedString {
var value = try! AttributedString(
markdown: "**AttributedText** supports [links](https://apple.com)."
)
value.font = UIFont.preferredFont(forTextStyle: .body)
value.foregroundColor = .label
return value
}
}
For plain text, you can also use an AttributedString string literal:
AttributedText(AttributedString("Hello, World!"))
SwiftUI.Text-compatible modifiers
The following SwiftUI text modifier is supported:
When lineLimit is not specified, text wraps across multiple lines. font(_:), bold(), truncationMode(_:), multilineTextAlignment(_:), lineSpacing(_:), and textCase(_:) are not applied. Set font and paragraph attributes in the input AttributedString instead.
Where to configure text attributes
AttributedText preserves the attributes in the input AttributedString. Configure values that belong to the paragraph or to the text content before passing the value to AttributedText:
| Value |
Configure on |
Notes |
| Paragraph alignment |
NSParagraphStyle.alignment |
Corresponds to multilineTextAlignment(_:). |
| Line break mode |
NSParagraphStyle.lineBreakMode |
Corresponds to truncationMode(_:). |
| Line spacing |
NSParagraphStyle.lineSpacing |
Corresponds to lineSpacing(_:). |
| Text case |
The input string |
Apply uppercasing or lowercasing before creating the AttributedString; textCase(_:) is not applied. |
| Font and color |
AttributedString attributes |
Set these explicitly; font values are not supplied by the view. |
The following value does not need to be added to the AttributedString:
lineLimit(_:) sets UITextView.textContainer.maximumNumberOfLines.
For example, configure paragraph attributes through NSParagraphStyle like this:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byTruncatingTail
paragraphStyle.lineSpacing = 4
let message = AttributedString(
NSAttributedString(
string: "A paragraph with custom layout",
attributes: [
.font: UIFont.preferredFont(forTextStyle: .body),
.paragraphStyle: paragraphStyle
]
)
)
AttributedText(message)
.lineLimit(3)
Rich text and selection
Attributes in an AttributedString and NSTextAttachment values are preserved when rendered.
let attachment = NSTextAttachment(image: UIImage(systemName: "star")!)
let value = AttributedString(NSAttributedString(attachment: attachment))
AttributedText(value)
.allowsSelectionTextItems([.link, .textAttachment])
The following text item types can be enabled for selection:
.link
.textAttachment
.tag
Actions
You can configure link and tag taps, copy handling, and additional edit menu actions.
AttributedText(message)
.onTapTextItemTag { tag in
print("Tapped tag: \(tag)")
}
.onCopy { copiedText in
print(copiedText)
}
.extraActions([
UIAction(title: "Custom Action") { _ in
// Handle the action.
}
])
Links are handled through SwiftUI's openURL environment value.
Example
Example.swiftpm contains a comparison screen that displays SwiftUI Text and AttributedText with the same modifiers side by side.
open Example.swiftpm
Test
xcodebuild -scheme AttributedText \
-destination 'platform=iOS Simulator,name=iPhone 17 Pro,OS=27.0' \
test
Performance and layout regression checks
PerformanceLab/AttributedTextPerformance.xcodeproj is a standalone iOS app and
XCTest target that renders 200 dynamic rich-text rows. It records clock, CPU, and
memory metrics for first layout and content updates, and checks the visible
UITextView count and heights against PerformanceBaselines/layout.json.
Run the complete XCTest plus Instruments Time Profiler workflow with a signed,
connected iPhone. The command retains the simulator XCTest metrics and records
the Time Profiler trace on the iPhone. Provide the device name shown by
xcrun xctrace list devices:
PERFORMANCE_INSTRUMENTS_DEVICE_NAME='My iPhone' Scripts/run-performance.sh
Set INSTRUMENTS_DURATION=30 (or 30s) to change the recording duration.
It stores the .xcresult, XCTest metric JSON, .trace, and the Instruments table
of contents under PerformanceResults/. It fails if Instruments cannot export a
complete trace, so CI cannot mistake a partial recording for a pass.
Compare two exported XCTest metric files with a 10% regression threshold (or pass
another threshold as the third argument):
Scripts/compare-performance.py baseline/xctest-metrics.json candidate/xctest-metrics.json 10
After an intentional visual/layout change, regenerate the layout baseline on a
simulator before committing it:
UPDATE_LAYOUT_BASELINE=1 xcodebuild test \
-project PerformanceLab/AttributedTextPerformance.xcodeproj \
-scheme AttributedTextPerformance \
-destination 'platform=iOS Simulator,name=iPhone 17 Pro'
License
MIT