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

Commit

Permalink
Consider line limit and truncation mode environment values
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Dec 26, 2020
1 parent 9e72b40 commit 6f889a3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "NO"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AttributedText"
BuildableName = "AttributedText"
BlueprintName = "AttributedText"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AttributedText"
BuildableName = "AttributedText"
BlueprintName = "AttributedText"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
18 changes: 18 additions & 0 deletions Sources/AttributedText/AttributedText_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions Sources/AttributedText/AttributedText_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions Sources/AttributedText/NSLineBreakMode+TruncationMode.swift
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6f889a3

Please sign in to comment.