Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Oct 5, 2021
1 parent c5b16ba commit 6622cac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
23 changes: 17 additions & 6 deletions Sources/AttributedText/AttributedText.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
#if !os(watchOS)
import SwiftUI

/// A view that displays styled attributed text.
public struct AttributedText: View {
@StateObject private var textSizeViewModel = TextSizeViewModel()

private let attributedText: NSAttributedString
private let openLink: ((URL) -> Void)?
private let onOpenLink: ((URL) -> Void)?

public init(_ attributedText: NSAttributedString, openLink: ((URL) -> Void)? = nil) {
/// Creates an attributed text view.
/// - Parameters:
/// - attributedText: An attributed string to display.
/// - onOpenLink: The action to perform when the user opens a link in the text. When not specified,
/// the view opens the links using the `OpenURLAction` from the environment.
public init(_ attributedText: NSAttributedString, onOpenLink: ((URL) -> Void)? = nil) {
self.attributedText = attributedText
self.openLink = openLink
self.onOpenLink = onOpenLink
}

public init(openLink: ((URL) -> Void)? = nil, attributedText: () -> NSAttributedString) {
self.init(attributedText(), openLink: openLink)
/// Creates an attributed text view.
/// - Parameters:
/// - attributedText: A closure that creates the attributed string to display.
/// - onOpenLink: The action to perform when the user opens a link in the text. When not specified,
/// the view opens the links using the `OpenURLAction` from the environment.
public init(attributedText: () -> NSAttributedString, onOpenLink: ((URL) -> Void)? = nil) {
self.init(attributedText(), onOpenLink: onOpenLink)
}

public var body: some View {
Expand All @@ -22,7 +33,7 @@
attributedText: attributedText,
maxLayoutWidth: geometry.maxWidth,
textSizeViewModel: textSizeViewModel,
openLink: openLink
onOpenLink: onOpenLink
)
}
.frame(
Expand Down
4 changes: 2 additions & 2 deletions Sources/AttributedText/AttributedTextImpl+AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
)

if #available(macOS 11.0, *) {
context.coordinator.openLink = openLink ?? { context.environment.openURL($0) }
context.coordinator.openLink = onOpenLink ?? { context.environment.openURL($0) }
} else {
context.coordinator.openLink = openLink
context.coordinator.openLink = onOpenLink
}

textSizeViewModel.didUpdateTextView(nsView)
Expand Down
4 changes: 2 additions & 2 deletions Sources/AttributedText/AttributedTextImpl+UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
)

if #available(iOS 14.0, tvOS 14.0, *) {
context.coordinator.openLink = openLink ?? { context.environment.openURL($0) }
context.coordinator.openLink = onOpenLink ?? { context.environment.openURL($0) }
} else {
context.coordinator.openLink = openLink
context.coordinator.openLink = onOpenLink
}

textSizeViewModel.didUpdateTextView(uiView)
Expand Down
2 changes: 1 addition & 1 deletion Sources/AttributedText/AttributedTextImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
var attributedText: NSAttributedString
var maxLayoutWidth: CGFloat
var textSizeViewModel: TextSizeViewModel
var openLink: ((URL) -> Void)?
var onOpenLink: ((URL) -> Void)?
}
#endif

0 comments on commit 6622cac

Please sign in to comment.