Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/ExpandableText/ExpandableText+Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
53 changes: 43 additions & 10 deletions Sources/ExpandableText/ExpandableText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,17 +86,17 @@ public struct ExpandableText: View {
withAnimation(expandAnimation) { isExpanded.toggle() }
}
}
.modifier(OverlayAdapter(alignment: .trailingLastTextBaseline, view: {
if shouldShowMoreButton {
Button {
withAnimation(expandAnimation) { isExpanded.toggle() }
} label: {
Text(moreButtonText)
.font(moreButtonFont ?? font)
.foregroundColor(moreButtonColor)
}
.modifier(OverlayAdapter(alignment: .trailingLastTextBaseline) {
if let moreButtonPressedColor {
button
.buttonStyle(
.pressedColorButton(moreButtonColor, moreButtonPressedColor)
)
} else {
button
.foregroundColor(moreButtonColor)
}
}))
})
}

private var content: some View {
Expand All @@ -109,6 +110,16 @@ 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)
}
.opacity(shouldShowMoreButton ? 1 : 0)
}

private var shouldShowMoreButton: Bool {
!isExpanded && isTruncated
}
Expand All @@ -117,3 +128,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
)
}
}
1 change: 1 addition & 0 deletions Sources/Utilities/TruncationTextMask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private struct TruncationTextMask: ViewModifier {
}.frame(height: size.height)
}
)
.fixedSize(horizontal: false, vertical: true)
} else {
content
.fixedSize(horizontal: false, vertical: true)
Expand Down