This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back the default implementation for tvOS
- Loading branch information
1 parent
287c40d
commit 7b2953f
Showing
3 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |