Ver Fonte

Prevent button text wrapping or truncation

Matthew Mathias há 4 anos atrás
pai
commit
73e294b04d

+ 6 - 0
GoogleSignInSwift/Sources/GoogleSignInButton.swift

@@ -65,7 +65,13 @@ public struct GoogleSignInButton: View {
           }
             .padding(.leading, 1)
           Text(viewModel.style.buttonText)
+            .fixedSize()
             .padding(.trailing, textPadding)
+            .frame(
+              width: viewModel.style.minWidthForButtonText,
+              height: buttonHeight,
+              alignment: .leading
+            )
           Spacer()
         }
       }

+ 31 - 7
GoogleSignInSwift/Sources/GoogleSignInButtonStyling.swift

@@ -50,10 +50,6 @@ let googleImageName = "google"
 /// The layout styles supported by the sign-in button.
 ///
 /// The minimum size of the button depends on the language used for text.
-/// The following dimensions (in points) fit for all languages:
-/// - standard: 230 x 48
-/// - wide:     312 x 48
-/// - icon:     48 x 48 (no text, fixed size)
 @available(iOS 13.0, macOS 10.15, *)
 public enum GoogleSignInButtonStyle {
   case standard
@@ -205,9 +201,13 @@ fileprivate struct Width {
 extension GoogleSignInButtonStyle {
   fileprivate var width: Width {
     switch self {
-    case .icon: return Width(min: iconWidth, max: iconWidth)
-    case .standard: return Width(min: 90, max: .infinity)
-    case .wide: return Width(min: 170, max: .infinity)
+    case .icon:
+      return Width(min: iconWidth, max: iconWidth)
+    case .standard, .wide:
+      return Width(
+        min: iconWidth + minWidthForButtonText + iconPadding + textPadding,
+        max: .infinity
+      )
     }
   }
 
@@ -222,6 +222,30 @@ extension GoogleSignInButtonStyle {
     case .icon: return ""
     }
   }
+
+  var minWidthForButtonText: CGFloat {
+    let bt = buttonText as NSString
+#if os(iOS) || targetEnvironment(macCatalyst)
+    let font = UIFont(name: fontNameRobotoBold, size: fontSize)
+    let rect = bt.boundingRect(
+      with: CGSize(width: .max, height: .max),
+      options: [],
+      attributes: [.font: font as Any],
+      context: nil
+    )
+#elseif os(macOS)
+    let font = NSFont(name: fontNameRobotoBold, size: fontSize)
+    let rect = bt.boundingRect(
+      with: CGSize(width: .max, height: .max),
+      options: [],
+      attributes: [.font: font as Any]
+    )
+#else
+    fatalError("Unrecognized platform to calculate minimum width")
+#endif
+
+    return rect.width
+  }
 }
 
 // MARK: - Button Style

+ 2 - 2
Samples/Swift/DaysUntilBirthday/Shared/Views/SignInView.swift

@@ -24,8 +24,8 @@ struct SignInView: View {
     VStack {
       HStack {
         let buttonViewModel = GoogleSignInButtonViewModel(
-          scheme: .light,
-          style: .standard
+          scheme: .dark,
+          style: .wide
         )
         GoogleSignInButton(
           viewModel: buttonViewModel,