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

Commit

Permalink
Bring back the default implementation for tvOS
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Dec 30, 2021
1 parent 287c40d commit 7b2953f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if canImport(UIKit)
#if os(iOS)
import SwiftUI

extension AttributedTextImpl: UIViewRepresentable {
Expand Down Expand Up @@ -35,9 +35,7 @@

self.backgroundColor = .clear
self.textContainerInset = .zero
#if !os(tvOS)
self.isEditable = false
#endif
self.isEditable = false
self.isSelectable = false
self.isScrollEnabled = false
self.textContainer.lineFragmentPadding = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
nsView.textContainer?.lineBreakMode = NSLineBreakMode(
truncationMode: context.environment.truncationMode
)

context.coordinator.openLink = onOpenLink ?? { context.environment.openURL($0) }
textSizeViewModel.didUpdateTextView(nsView)
}
Expand Down
84 changes: 84 additions & 0 deletions Sources/AttributedText/AttributedTextImpl+tvOS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#if os(tvOS)
import SwiftUI

extension AttributedTextImpl: UIViewRepresentable {
func makeUIView(context: Context) -> TextView {
let uiView = TextView()

uiView.backgroundColor = .clear
uiView.textContainerInset = .zero
uiView.isScrollEnabled = false
uiView.textContainer.lineFragmentPadding = 0
uiView.delegate = context.coordinator

return uiView
}

func updateUIView(_ uiView: TextView, context: Context) {
uiView.attributedText = attributedText
uiView.maxLayoutWidth = maxLayoutWidth

uiView.textContainer.maximumNumberOfLines = context.environment.lineLimit ?? 0
uiView.textContainer.lineBreakMode = NSLineBreakMode(
truncationMode: context.environment.truncationMode
)
context.coordinator.openLink = onOpenLink ?? { context.environment.openURL($0) }
textSizeViewModel.didUpdateTextView(uiView)
}

func makeCoordinator() -> Coordinator {
Coordinator()
}
}

extension AttributedTextImpl {
final class TextView: UITextView {
var maxLayoutWidth: CGFloat = 0 {
didSet {
guard maxLayoutWidth != oldValue else { return }
invalidateIntrinsicContentSize()
}
}

override var intrinsicContentSize: CGSize {
guard maxLayoutWidth > 0 else {
return super.intrinsicContentSize
}

return sizeThatFits(CGSize(width: maxLayoutWidth, height: .greatestFiniteMagnitude))
}
}

final class Coordinator: NSObject, UITextViewDelegate {
var openLink: ((URL) -> Void)?

func textView(
_: UITextView,
shouldInteractWith URL: URL,
in _: NSRange,
interaction: UITextItemInteraction
) -> Bool {
guard case .invokeDefaultAction = interaction else {
return false
}

if let openLink = self.openLink {
openLink(URL)
return false
} else {
return true
}
}

func textView(
_: UITextView,
shouldInteractWith _: NSTextAttachment,
in _: NSRange,
interaction _: UITextItemInteraction
) -> Bool {
// Disable text attachment interactions
false
}
}
}
#endif

0 comments on commit 7b2953f

Please sign in to comment.