Skip to content

Commit b44618c

Browse files
authored
Prevent button text wrapping or truncation (google#121)
1 parent 6c1b06b commit b44618c

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

GoogleSignInSwift/Sources/GoogleSignInButton.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ public struct GoogleSignInButton: View {
6363
)
6464
Image.signInButtonImage
6565
}
66-
.padding(.leading, 1)
66+
.padding(.leading, 1)
6767
Text(viewModel.style.buttonText)
68+
.fixedSize()
6869
.padding(.trailing, textPadding)
70+
.frame(
71+
width: viewModel.style.widthForButtonText,
72+
height: buttonHeight,
73+
alignment: .leading
74+
)
6975
Spacer()
7076
}
7177
}

GoogleSignInSwift/Sources/GoogleSignInButtonStyling.swift

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ let googleImageName = "google"
5050
/// The layout styles supported by the sign-in button.
5151
///
5252
/// The minimum size of the button depends on the language used for text.
53-
/// The following dimensions (in points) fit for all languages:
54-
/// - standard: 230 x 48
55-
/// - wide: 312 x 48
56-
/// - icon: 48 x 48 (no text, fixed size)
5753
@available(iOS 13.0, macOS 10.15, *)
5854
public enum GoogleSignInButtonStyle {
5955
case standard
@@ -205,9 +201,13 @@ fileprivate struct Width {
205201
extension GoogleSignInButtonStyle {
206202
fileprivate var width: Width {
207203
switch self {
208-
case .icon: return Width(min: iconWidth, max: iconWidth)
209-
case .standard: return Width(min: 90, max: .infinity)
210-
case .wide: return Width(min: 170, max: .infinity)
204+
case .icon:
205+
return Width(min: iconWidth, max: iconWidth)
206+
case .standard, .wide:
207+
return Width(
208+
min: iconWidth + widthForButtonText + iconPadding + textPadding,
209+
max: .infinity
210+
)
211211
}
212212
}
213213

@@ -222,6 +222,28 @@ extension GoogleSignInButtonStyle {
222222
case .icon: return ""
223223
}
224224
}
225+
226+
var widthForButtonText: CGFloat {
227+
let bt = buttonText as NSString
228+
let size = CGSize(width: .max, height: .max)
229+
let anyFont: Any
230+
#if os(iOS) || targetEnvironment(macCatalyst)
231+
anyFont = UIFont(name: fontNameRobotoBold, size: fontSize) as Any
232+
#elseif os(macOS)
233+
anyFont = NSFont(name: fontNameRobotoBold, size: fontSize) as Any
234+
#else
235+
fatalError("Unrecognized platform to calculate minimum width")
236+
#endif
237+
238+
let rect = bt.boundingRect(
239+
with: size,
240+
options: [],
241+
attributes: [.font: anyFont],
242+
context: nil
243+
)
244+
245+
return rect.width
246+
}
225247
}
226248

227249
// MARK: - Button Style

0 commit comments

Comments
 (0)