From 6f889a35d2b2324227b1b5ecc1d10d3c85a8fc3d Mon Sep 17 00:00:00 2001 From: Guille Gonzalez Date: Sat, 26 Dec 2020 10:43:07 +0100 Subject: [PATCH] Consider line limit and truncation mode environment values --- .../xcschemes/AttributedText_watchOS.xcscheme | 67 +++++++++++++++++++ .../AttributedText_AppKit.swift | 18 +++++ .../AttributedText/AttributedText_UIKit.swift | 18 +++++ .../NSLineBreakMode+TruncationMode.swift | 21 ++++++ 4 files changed, 124 insertions(+) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/AttributedText_watchOS.xcscheme create mode 100644 Sources/AttributedText/NSLineBreakMode+TruncationMode.swift diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/AttributedText_watchOS.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/AttributedText_watchOS.xcscheme new file mode 100644 index 0000000..32040fc --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/AttributedText_watchOS.xcscheme @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/AttributedText/AttributedText_AppKit.swift b/Sources/AttributedText/AttributedText_AppKit.swift index 6c5de54..5db30e2 100644 --- a/Sources/AttributedText/AttributedText_AppKit.swift +++ b/Sources/AttributedText/AttributedText_AppKit.swift @@ -15,6 +15,8 @@ func updateNSView(_ nsView: AttributedTextView, context: Context) { nsView.attributedText = attributedText nsView.preferredMaxLayoutWidth = preferredMaxLayoutWidth + nsView.numberOfLines = context.environment.lineLimit ?? 0 + nsView.lineBreakMode = NSLineBreakMode(truncationMode: context.environment.truncationMode) nsView.openURL = context.environment.openURL DispatchQueue.main.async { @@ -47,6 +49,22 @@ } } + var numberOfLines: Int { + get { textView.textContainer?.maximumNumberOfLines ?? 0 } + set { + textView.textContainer?.maximumNumberOfLines = newValue + invalidateIntrinsicContentSize() + } + } + + var lineBreakMode: NSLineBreakMode { + get { textView.textContainer?.lineBreakMode ?? .byWordWrapping } + set { + textView.textContainer?.lineBreakMode = newValue + invalidateIntrinsicContentSize() + } + } + var openURL: OpenURLAction? override var intrinsicContentSize: CGSize { diff --git a/Sources/AttributedText/AttributedText_UIKit.swift b/Sources/AttributedText/AttributedText_UIKit.swift index f6772c9..7a497c1 100644 --- a/Sources/AttributedText/AttributedText_UIKit.swift +++ b/Sources/AttributedText/AttributedText_UIKit.swift @@ -15,6 +15,8 @@ func updateUIView(_ uiView: AttributedTextView, context: Context) { uiView.attributedText = attributedText uiView.preferredMaxLayoutWidth = preferredMaxLayoutWidth + uiView.numberOfLines = context.environment.lineLimit ?? 0 + uiView.lineBreakMode = NSLineBreakMode(truncationMode: context.environment.truncationMode) uiView.openURL = context.environment.openURL DispatchQueue.main.async { @@ -43,6 +45,22 @@ } } + var numberOfLines: Int { + get { textView.textContainer.maximumNumberOfLines } + set { + textView.textContainer.maximumNumberOfLines = newValue + invalidateIntrinsicContentSize() + } + } + + var lineBreakMode: NSLineBreakMode { + get { textView.textContainer.lineBreakMode } + set { + textView.textContainer.lineBreakMode = newValue + invalidateIntrinsicContentSize() + } + } + var openURL: OpenURLAction? override var intrinsicContentSize: CGSize { diff --git a/Sources/AttributedText/NSLineBreakMode+TruncationMode.swift b/Sources/AttributedText/NSLineBreakMode+TruncationMode.swift new file mode 100644 index 0000000..875a55c --- /dev/null +++ b/Sources/AttributedText/NSLineBreakMode+TruncationMode.swift @@ -0,0 +1,21 @@ +#if canImport(SwiftUI) && !os(watchOS) + + import SwiftUI + + extension NSLineBreakMode { + @available(macOS 11.0, iOS 14.0, tvOS 14.0, *) + init(truncationMode: Text.TruncationMode) { + switch truncationMode { + case .head: + self = .byTruncatingHead + case .tail: + self = .byTruncatingTail + case .middle: + self = .byTruncatingMiddle + @unknown default: + self = .byWordWrapping + } + } + } + +#endif