diff --git a/Sources/AttributedText/AttributedText.swift b/Sources/AttributedText/AttributedText.swift index 15513e1..04e30e3 100644 --- a/Sources/AttributedText/AttributedText.swift +++ b/Sources/AttributedText/AttributedText.swift @@ -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 +} diff --git a/Sources/AttributedText/AttributedTextImpl+iOS.swift b/Sources/AttributedText/AttributedTextImpl+iOS.swift index dc7b281..bb65695 100644 --- a/Sources/AttributedText/AttributedTextImpl+iOS.swift +++ b/Sources/AttributedText/AttributedTextImpl+iOS.swift @@ -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 diff --git a/Sources/AttributedText/AttributedTextImpl+macOS.swift b/Sources/AttributedText/AttributedTextImpl+macOS.swift index bb0f869..f1fb6c8 100644 --- a/Sources/AttributedText/AttributedTextImpl+macOS.swift +++ b/Sources/AttributedText/AttributedTextImpl+macOS.swift @@ -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 diff --git a/Sources/AttributedText/AttributedTextImpl+tvOS.swift b/Sources/AttributedText/AttributedTextImpl+tvOS.swift index 554f281..c93c2d5 100644 --- a/Sources/AttributedText/AttributedTextImpl+tvOS.swift +++ b/Sources/AttributedText/AttributedTextImpl+tvOS.swift @@ -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