Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/AttributedText/AttributedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ extension GeometryProxy {
size.width - safeAreaInsets.leading - safeAreaInsets.trailing
}
}

extension View {
/// Set the attributes used to draw the onscreen presentation of link text.
/// - Parameter linkTextAttributes: The attributes used to draw the onscreen presentation of link text.
/// - Returns: A view with the linkTextAttributes set to the value you supply.
public func linkTextAttributes(_ linkTextAttributes: [NSAttributedString.Key: Any]) -> some View {
environment(\.linkTextAttributes, linkTextAttributes)
}

/// Set the attributes used to draw the onscreen presentation of link text.
/// - Parameter linkTextAttributes: A closure that creates the attributes used to draw the onscreen presentation of link text.
/// - Returns: A view with the linkTextAttributes set to the value you supply.
public func linkTextAttributes(_ linkTextAttributes: () -> [NSAttributedString.Key: Any]) -> some View {
environment(\.linkTextAttributes, linkTextAttributes())
}
}

extension EnvironmentValues {
internal var linkTextAttributes: [NSAttributedString.Key: Any]? {
get { self[LinkTextAttributesKey.self] }
set { self[LinkTextAttributesKey.self] = newValue }
}
}

private struct LinkTextAttributesKey: EnvironmentKey {
static let defaultValue: [NSAttributedString.Key: Any]? = nil
}
4 changes: 4 additions & 0 deletions Sources/AttributedText/AttributedTextImpl+iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
uiView.attributedText = attributedText
uiView.maxLayoutWidth = maxLayoutWidth

if let linkTextAttributes = context.environment.linkTextAttributes {
uiView.linkTextAttributes = linkTextAttributes
}

uiView.textContainer.maximumNumberOfLines = context.environment.lineLimit ?? 0
uiView.textContainer.lineBreakMode = NSLineBreakMode(
truncationMode: context.environment.truncationMode
Expand Down
4 changes: 4 additions & 0 deletions Sources/AttributedText/AttributedTextImpl+macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
nsView.textStorage?.setAttributedString(attributedText)
nsView.maxLayoutWidth = maxLayoutWidth

if let linkTextAttributes = context.environment.linkTextAttributes {
nsView.linkTextAttributes = linkTextAttributes
}

nsView.textContainer?.maximumNumberOfLines = context.environment.lineLimit ?? 0
nsView.textContainer?.lineBreakMode = NSLineBreakMode(
truncationMode: context.environment.truncationMode
Expand Down
4 changes: 4 additions & 0 deletions Sources/AttributedText/AttributedTextImpl+tvOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
uiView.attributedText = attributedText
uiView.maxLayoutWidth = maxLayoutWidth

if let linkTextAttributes = context.environment.linkTextAttributes {
uiView.linkTextAttributes = linkTextAttributes
}

uiView.textContainer.maximumNumberOfLines = context.environment.lineLimit ?? 0
uiView.textContainer.lineBreakMode = NSLineBreakMode(
truncationMode: context.environment.truncationMode
Expand Down