From cbd704c3e065f1b1ba6855f8e66cc1eb36a5f583 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 22 Sep 2024 21:01:39 -0500 Subject: [PATCH 1/2] Add option to set more button pressed color Also fix intrinsic size issue on UIKit side --- .../ExpandableText+Modifiers.swift | 11 +++++ Sources/ExpandableText/ExpandableText.swift | 44 ++++++++++++++++--- Sources/Utilities/TruncationTextMask.swift | 1 + 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/Sources/ExpandableText/ExpandableText+Modifiers.swift b/Sources/ExpandableText/ExpandableText+Modifiers.swift index d7cb4ee..c2c6c53 100644 --- a/Sources/ExpandableText/ExpandableText+Modifiers.swift +++ b/Sources/ExpandableText/ExpandableText+Modifiers.swift @@ -76,6 +76,17 @@ public extension ExpandableText { return copy } + /** + Sets the pressed color to use for the "show more" button in the `ExpandableText` instance. + - Parameter color: The color to use for the "show more" button pressed state. + - Returns: A new `ExpandableText` instance with the specified "show more" button pressed color applied. + */ + func moreButtonPressedColor(_ color: Color) -> Self { + var copy = self + copy.moreButtonPressedColor = color + return copy + } + /** Sets the animation to use when expanding the `ExpandableText` instance. - Parameter animation: The animation to use for the expansion. Defaults to `default` diff --git a/Sources/ExpandableText/ExpandableText.swift b/Sources/ExpandableText/ExpandableText.swift index 43cdff0..485b57d 100644 --- a/Sources/ExpandableText/ExpandableText.swift +++ b/Sources/ExpandableText/ExpandableText.swift @@ -41,6 +41,7 @@ public struct ExpandableText: View { internal var moreButtonText: String = "more" internal var moreButtonFont: Font? internal var moreButtonColor: Color = .accentColor + internal var moreButtonPressedColor: Color? internal var expandAnimation: Animation = .default internal var collapseEnabled: Bool = false internal var trimMultipleNewlinesWhenTruncated: Bool = true @@ -87,11 +88,13 @@ public struct ExpandableText: View { } .modifier(OverlayAdapter(alignment: .trailingLastTextBaseline, view: { if shouldShowMoreButton { - Button { - withAnimation(expandAnimation) { isExpanded.toggle() } - } label: { - Text(moreButtonText) - .font(moreButtonFont ?? font) + if let moreButtonPressedColor { + button + .buttonStyle( + .pressedColorButton(moreButtonColor, moreButtonPressedColor) + ) + } else { + button .foregroundColor(moreButtonColor) } } @@ -109,6 +112,15 @@ public struct ExpandableText: View { .frame(maxWidth: .infinity, alignment: .leading) } + private var button: some View { + Button { + withAnimation(expandAnimation) { isExpanded.toggle() } + } label: { + Text(moreButtonText) + .font(moreButtonFont ?? font) + } + } + private var shouldShowMoreButton: Bool { !isExpanded && isTruncated } @@ -117,3 +129,25 @@ public struct ExpandableText: View { text.replacingOccurrences(of: #"\n\s*\n"#, with: "\n", options: .regularExpression) } } + +internal struct PressedColorButton: ButtonStyle { + let moreButtonColor: Color + let moreButtonPressedColor: Color + + func makeBody(configuration: Configuration) -> some View { + configuration.label + .foregroundColor(configuration.isPressed ? moreButtonPressedColor : moreButtonColor) + } +} + +internal extension ButtonStyle where Self == PressedColorButton { + static func pressedColorButton( + _ moreButtonColor: Color, + _ moreButtonPressedColor: Color + ) -> Self { + Self( + moreButtonColor: moreButtonColor, + moreButtonPressedColor: moreButtonPressedColor + ) + } +} diff --git a/Sources/Utilities/TruncationTextMask.swift b/Sources/Utilities/TruncationTextMask.swift index faa2e37..f040397 100644 --- a/Sources/Utilities/TruncationTextMask.swift +++ b/Sources/Utilities/TruncationTextMask.swift @@ -40,6 +40,7 @@ private struct TruncationTextMask: ViewModifier { }.frame(height: size.height) } ) + .fixedSize(horizontal: false, vertical: true) } else { content .fixedSize(horizontal: false, vertical: true) From d6c8f81494e028b4ff3abc2bd361b7a93c56c2fd Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 27 Sep 2024 17:49:35 -0500 Subject: [PATCH 2/2] Use opacity modifier over branch logic to show/hide more button --- Sources/ExpandableText/ExpandableText.swift | 23 ++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Sources/ExpandableText/ExpandableText.swift b/Sources/ExpandableText/ExpandableText.swift index 485b57d..9a50ca3 100644 --- a/Sources/ExpandableText/ExpandableText.swift +++ b/Sources/ExpandableText/ExpandableText.swift @@ -86,19 +86,17 @@ public struct ExpandableText: View { withAnimation(expandAnimation) { isExpanded.toggle() } } } - .modifier(OverlayAdapter(alignment: .trailingLastTextBaseline, view: { - if shouldShowMoreButton { - if let moreButtonPressedColor { - button - .buttonStyle( - .pressedColorButton(moreButtonColor, moreButtonPressedColor) - ) - } else { - button - .foregroundColor(moreButtonColor) - } + .modifier(OverlayAdapter(alignment: .trailingLastTextBaseline) { + if let moreButtonPressedColor { + button + .buttonStyle( + .pressedColorButton(moreButtonColor, moreButtonPressedColor) + ) + } else { + button + .foregroundColor(moreButtonColor) } - })) + }) } private var content: some View { @@ -119,6 +117,7 @@ public struct ExpandableText: View { Text(moreButtonText) .font(moreButtonFont ?? font) } + .opacity(shouldShowMoreButton ? 1 : 0) } private var shouldShowMoreButton: Bool {