From cd5cf1b255d67a1f5b3e820d4da4cdc1d4491409 Mon Sep 17 00:00:00 2001 From: Brendan Lensink Date: Fri, 13 Feb 2026 11:46:44 -0800 Subject: [PATCH 1/2] add support for left and right aligned buttons --- .../View/CustomizingComponentsView.swift | 4 ++++ .../NiceComponents/Button/NiceButton.swift | 22 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift b/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift index e36dad1..31b50d4 100644 --- a/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift +++ b/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift @@ -63,6 +63,10 @@ struct CustomizingComponentsView: View { NiceButton("Over here as well", style: .secondary, rightImage: NiceButtonImage(systemIcon: "heart"), horizontalContentPadding: 20) {} + NiceButton("Leading aligned", style: .primary, contentHorizontalAlignment: .leading) {} + + NiceButton(".. and trailing aligned", style: .secondary, contentHorizontalAlignment: .trailing) {} + NiceButton("and buttons with images", style: .primary, balanceImages: false) {} .withLeftImage( diff --git a/Sources/NiceComponents/Button/NiceButton.swift b/Sources/NiceComponents/Button/NiceButton.swift index 5d796ab..5a3be55 100644 --- a/Sources/NiceComponents/Button/NiceButton.swift +++ b/Sources/NiceComponents/Button/NiceButton.swift @@ -16,10 +16,14 @@ public struct NiceButton: View { /// The style configuration for the button. let style: NiceButtonStyle - /// Padding between the button text/images and edges of the button. Default is 8. + /// Padding between the button text/images and edges of the button. + /// Default is nil, unless contentHorizontalAlignment has been set, in which case it is 16. /// Set this to `nil` to have the button fill it's available space, like you'd set `maxWidth: .infinity`. var horizontalContentPadding: CGFloat? + /// When set, aligns the content of the button, including images inside the button. Default is `center`. + var contentHorizontalAlignment: TextAlignment + /// An optional image to display on the left side of the button. var leftImage: NiceButtonImage? @@ -42,15 +46,18 @@ public struct NiceButton: View { /// - style: The style configuration for the button. /// - inactive: A Boolean value that determines whether the button is inactive. Defaults to `false`. /// - balanceImages: A Boolean value indicating whether the images should be balanced. Defaults to `true`. + /// - contentHorizontalAlignment: Optionally align all content within the button. Defaults to `center`. /// - leftImage: An optional image to display on the left side of the button. /// - rightImage: An optional image to display on the right side of the button. - /// - horizontalContentPadding: Padding between the button content and edges of the button. Default is nil, causing the button to expand to fill all available space. + /// - horizontalContentPadding: Padding between the button content and edges of the button. + /// Default is nil, unless contentHorizontalAlignment has been set, in which case it is 16. /// - action: The closure to execute when the button is tapped. public init( _ text: String, style: NiceButtonStyle, inactive: Bool = false, balanceImages: Bool = true, + contentHorizontalAlignment: TextAlignment? = nil, leftImage: NiceButtonImage? = nil, rightImage: NiceButtonImage? = nil, horizontalContentPadding: CGFloat? = nil, @@ -60,9 +67,10 @@ public struct NiceButton: View { self.style = style self.inactive = inactive self.balanceImages = balanceImages + self.contentHorizontalAlignment = contentHorizontalAlignment ?? .center self.leftImage = leftImage self.rightImage = rightImage - self.horizontalContentPadding = horizontalContentPadding + self.horizontalContentPadding = horizontalContentPadding ?? (contentHorizontalAlignment == nil ? nil : 16) self.action = action } @@ -73,6 +81,10 @@ public struct NiceButton: View { public var body: some View { Button(action: action) { HStack(spacing: 0) { + if contentHorizontalAlignment == .trailing { + Spacer() + } + if let leftImage = leftImage { leftImage.image .padding(.leading, leftImage.offset) @@ -92,6 +104,10 @@ public struct NiceButton: View { rightImage.image .padding(.trailing, rightImage.offset) } + + if contentHorizontalAlignment == .leading { + Spacer() + } } .frame(maxWidth: horizontalContentPadding == nil ? .infinity : nil) } From b487af9c926d9d9f796c5d658bd7dca71c27649b Mon Sep 17 00:00:00 2001 From: Brendan Lensink Date: Fri, 13 Feb 2026 12:42:52 -0800 Subject: [PATCH 2/2] bye bye extra space --- .../NiceComponentsExample/View/CustomizingComponentsView.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift b/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift index 31b50d4..fa77898 100644 --- a/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift +++ b/NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift @@ -67,7 +67,6 @@ struct CustomizingComponentsView: View { NiceButton(".. and trailing aligned", style: .secondary, contentHorizontalAlignment: .trailing) {} - NiceButton("and buttons with images", style: .primary, balanceImages: false) {} .withLeftImage( NiceImage(systemIcon: "fireworks", width: 25, height: 25),