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.
- Loading branch information
1 parent
6622cac
commit 744dc58
Showing
8 changed files
with
67 additions
and
151 deletions.
There are no files selected for viewing
67 changes: 0 additions & 67 deletions
67
.swiftpm/xcode/xcshareddata/xcschemes/AttributedText_watchOS.xcscheme
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,52 +1,50 @@ | ||
#if !os(watchOS) | ||
import SwiftUI | ||
import SwiftUI | ||
|
||
/// A view that displays styled attributed text. | ||
public struct AttributedText: View { | ||
@StateObject private var textSizeViewModel = TextSizeViewModel() | ||
/// A view that displays styled attributed text. | ||
public struct AttributedText: View { | ||
@StateObject private var textSizeViewModel = TextSizeViewModel() | ||
|
||
private let attributedText: NSAttributedString | ||
private let onOpenLink: ((URL) -> Void)? | ||
private let attributedText: NSAttributedString | ||
private let onOpenLink: ((URL) -> Void)? | ||
|
||
/// Creates an attributed text view. | ||
/// - Parameters: | ||
/// - attributedText: An attributed string to display. | ||
/// - onOpenLink: The action to perform when the user opens a link in the text. When not specified, | ||
/// the view opens the links using the `OpenURLAction` from the environment. | ||
public init(_ attributedText: NSAttributedString, onOpenLink: ((URL) -> Void)? = nil) { | ||
self.attributedText = attributedText | ||
self.onOpenLink = onOpenLink | ||
} | ||
/// Creates an attributed text view. | ||
/// - Parameters: | ||
/// - attributedText: An attributed string to display. | ||
/// - onOpenLink: The action to perform when the user opens a link in the text. When not specified, | ||
/// the view opens the links using the `OpenURLAction` from the environment. | ||
public init(_ attributedText: NSAttributedString, onOpenLink: ((URL) -> Void)? = nil) { | ||
self.attributedText = attributedText | ||
self.onOpenLink = onOpenLink | ||
} | ||
|
||
/// Creates an attributed text view. | ||
/// - Parameters: | ||
/// - attributedText: A closure that creates the attributed string to display. | ||
/// - onOpenLink: The action to perform when the user opens a link in the text. When not specified, | ||
/// the view opens the links using the `OpenURLAction` from the environment. | ||
public init(attributedText: () -> NSAttributedString, onOpenLink: ((URL) -> Void)? = nil) { | ||
self.init(attributedText(), onOpenLink: onOpenLink) | ||
} | ||
/// Creates an attributed text view. | ||
/// - Parameters: | ||
/// - attributedText: A closure that creates the attributed string to display. | ||
/// - onOpenLink: The action to perform when the user opens a link in the text. When not specified, | ||
/// the view opens the links using the `OpenURLAction` from the environment. | ||
public init(attributedText: () -> NSAttributedString, onOpenLink: ((URL) -> Void)? = nil) { | ||
self.init(attributedText(), onOpenLink: onOpenLink) | ||
} | ||
|
||
public var body: some View { | ||
GeometryReader { geometry in | ||
AttributedTextImpl( | ||
attributedText: attributedText, | ||
maxLayoutWidth: geometry.maxWidth, | ||
textSizeViewModel: textSizeViewModel, | ||
onOpenLink: onOpenLink | ||
) | ||
} | ||
.frame( | ||
idealWidth: textSizeViewModel.textSize?.width, | ||
idealHeight: textSizeViewModel.textSize?.height | ||
public var body: some View { | ||
GeometryReader { geometry in | ||
AttributedTextImpl( | ||
attributedText: attributedText, | ||
maxLayoutWidth: geometry.maxWidth, | ||
textSizeViewModel: textSizeViewModel, | ||
onOpenLink: onOpenLink | ||
) | ||
.fixedSize(horizontal: false, vertical: true) | ||
} | ||
.frame( | ||
idealWidth: textSizeViewModel.textSize?.width, | ||
idealHeight: textSizeViewModel.textSize?.height | ||
) | ||
.fixedSize(horizontal: false, vertical: true) | ||
} | ||
} | ||
|
||
extension GeometryProxy { | ||
fileprivate var maxWidth: CGFloat { | ||
size.width - safeAreaInsets.leading - safeAreaInsets.trailing | ||
} | ||
extension GeometryProxy { | ||
fileprivate var maxWidth: CGFloat { | ||
size.width - safeAreaInsets.leading - safeAreaInsets.trailing | ||
} | ||
#endif | ||
} |
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
#if !os(watchOS) | ||
import SwiftUI | ||
import SwiftUI | ||
|
||
struct AttributedTextImpl { | ||
var attributedText: NSAttributedString | ||
var maxLayoutWidth: CGFloat | ||
var textSizeViewModel: TextSizeViewModel | ||
var onOpenLink: ((URL) -> Void)? | ||
} | ||
#endif | ||
struct AttributedTextImpl { | ||
var attributedText: NSAttributedString | ||
var maxLayoutWidth: CGFloat | ||
var textSizeViewModel: TextSizeViewModel | ||
var onOpenLink: ((URL) -> Void)? | ||
} |
28 changes: 13 additions & 15 deletions
28
Sources/AttributedText/NSLineBreakMode+TruncationMode.swift
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 |
---|---|---|
@@ -1,18 +1,16 @@ | ||
#if !os(watchOS) | ||
import SwiftUI | ||
import SwiftUI | ||
|
||
extension NSLineBreakMode { | ||
init(truncationMode: Text.TruncationMode) { | ||
switch truncationMode { | ||
case .head: | ||
self = .byTruncatingHead | ||
case .tail: | ||
self = .byTruncatingTail | ||
case .middle: | ||
self = .byTruncatingMiddle | ||
@unknown default: | ||
self = .byWordWrapping | ||
} | ||
extension NSLineBreakMode { | ||
init(truncationMode: Text.TruncationMode) { | ||
switch truncationMode { | ||
case .head: | ||
self = .byTruncatingHead | ||
case .tail: | ||
self = .byTruncatingTail | ||
case .middle: | ||
self = .byTruncatingMiddle | ||
@unknown default: | ||
self = .byWordWrapping | ||
} | ||
} | ||
#endif | ||
} |
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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
#if !os(watchOS) | ||
import SwiftUI | ||
import SwiftUI | ||
|
||
final class TextSizeViewModel: ObservableObject { | ||
@Published var textSize: CGSize? | ||
final class TextSizeViewModel: ObservableObject { | ||
@Published var textSize: CGSize? | ||
|
||
func didUpdateTextView(_ textView: AttributedTextImpl.TextView) { | ||
textSize = textView.intrinsicContentSize | ||
} | ||
func didUpdateTextView(_ textView: AttributedTextImpl.TextView) { | ||
textSize = textView.intrinsicContentSize | ||
} | ||
#endif | ||
} |