Skip to content

Commit 6eb5f43

Browse files
add support for left and right aligned buttons (#73)
* add support for left and right aligned buttons * bye bye extra space
1 parent 1f64e23 commit 6eb5f43

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

NiceComponentsExample/NiceComponentsExample/View/CustomizingComponentsView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ struct CustomizingComponentsView: View {
6363

6464
NiceButton("Over here as well", style: .secondary, rightImage: NiceButtonImage(systemIcon: "heart"), horizontalContentPadding: 20) {}
6565

66+
NiceButton("Leading aligned", style: .primary, contentHorizontalAlignment: .leading) {}
67+
68+
NiceButton(".. and trailing aligned", style: .secondary, contentHorizontalAlignment: .trailing) {}
6669

6770
NiceButton("and buttons with images", style: .primary, balanceImages: false) {}
6871
.withLeftImage(

Sources/NiceComponents/Button/NiceButton.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ public struct NiceButton: View {
1616
/// The style configuration for the button.
1717
let style: NiceButtonStyle
1818

19-
/// Padding between the button text/images and edges of the button. Default is 8.
19+
/// Padding between the button text/images and edges of the button.
20+
/// Default is nil, unless contentHorizontalAlignment has been set, in which case it is 16.
2021
/// Set this to `nil` to have the button fill it's available space, like you'd set `maxWidth: .infinity`.
2122
var horizontalContentPadding: CGFloat?
2223

24+
/// When set, aligns the content of the button, including images inside the button. Default is `center`.
25+
var contentHorizontalAlignment: TextAlignment
26+
2327
/// An optional image to display on the left side of the button.
2428
var leftImage: NiceButtonImage?
2529

@@ -42,15 +46,18 @@ public struct NiceButton: View {
4246
/// - style: The style configuration for the button.
4347
/// - inactive: A Boolean value that determines whether the button is inactive. Defaults to `false`.
4448
/// - balanceImages: A Boolean value indicating whether the images should be balanced. Defaults to `true`.
49+
/// - contentHorizontalAlignment: Optionally align all content within the button. Defaults to `center`.
4550
/// - leftImage: An optional image to display on the left side of the button.
4651
/// - rightImage: An optional image to display on the right side of the button.
47-
/// - horizontalContentPadding: Padding between the button content and edges of the button. Default is nil, causing the button to expand to fill all available space.
52+
/// - horizontalContentPadding: Padding between the button content and edges of the button.
53+
/// Default is nil, unless contentHorizontalAlignment has been set, in which case it is 16.
4854
/// - action: The closure to execute when the button is tapped.
4955
public init(
5056
_ text: String,
5157
style: NiceButtonStyle,
5258
inactive: Bool = false,
5359
balanceImages: Bool = true,
60+
contentHorizontalAlignment: TextAlignment? = nil,
5461
leftImage: NiceButtonImage? = nil,
5562
rightImage: NiceButtonImage? = nil,
5663
horizontalContentPadding: CGFloat? = nil,
@@ -60,9 +67,10 @@ public struct NiceButton: View {
6067
self.style = style
6168
self.inactive = inactive
6269
self.balanceImages = balanceImages
70+
self.contentHorizontalAlignment = contentHorizontalAlignment ?? .center
6371
self.leftImage = leftImage
6472
self.rightImage = rightImage
65-
self.horizontalContentPadding = horizontalContentPadding
73+
self.horizontalContentPadding = horizontalContentPadding ?? (contentHorizontalAlignment == nil ? nil : 16)
6674
self.action = action
6775
}
6876

@@ -73,6 +81,10 @@ public struct NiceButton: View {
7381
public var body: some View {
7482
Button(action: action) {
7583
HStack(spacing: 0) {
84+
if contentHorizontalAlignment == .trailing {
85+
Spacer()
86+
}
87+
7688
if let leftImage = leftImage {
7789
leftImage.image
7890
.padding(.leading, leftImage.offset)
@@ -92,6 +104,10 @@ public struct NiceButton: View {
92104
rightImage.image
93105
.padding(.trailing, rightImage.offset)
94106
}
107+
108+
if contentHorizontalAlignment == .leading {
109+
Spacer()
110+
}
95111
}
96112
.frame(maxWidth: horizontalContentPadding == nil ? .infinity : nil)
97113
}

0 commit comments

Comments
 (0)