Download this file
| 1 | #if os(iOS) |
| 2 | import SwiftUI |
| 3 | import UIKit |
| 4 | |
| 5 | /// UIKit supplies character wrapping without inserting characters into the |
| 6 | /// log, so selection and copying preserve the exact stored text. |
| 7 | struct WrappingLogText: UIViewRepresentable { |
| 8 | let text: String |
| 9 | |
| 10 | func makeUIView(context: Context) -> UITextView { |
| 11 | let view = UITextView() |
| 12 | view.isEditable = false |
| 13 | view.isSelectable = true |
| 14 | view.isScrollEnabled = false |
| 15 | view.backgroundColor = .clear |
| 16 | view.textContainerInset = .zero |
| 17 | view.textContainer.lineFragmentPadding = 0 |
| 18 | view.textContainer.lineBreakMode = .byCharWrapping |
| 19 | view.adjustsFontForContentSizeCategory = true |
| 20 | return view |
| 21 | } |
| 22 | |
| 23 | func updateUIView(_ view: UITextView, context: Context) { |
| 24 | view.text = text |
| 25 | view.textColor = .label |
| 26 | view.font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .monospacedSystemFont(ofSize: 12, weight: .regular)) |
| 27 | } |
| 28 | |
| 29 | func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? { |
| 30 | guard let width = proposal.width else { return nil } |
| 31 | return uiView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)) |
| 32 | } |
| 33 | } |
| 34 | #endif |
| 35 | |