Style the card fields
Set the font, colors, border, corners, and padding of the SDK's card inputs, with their own looks while focused or invalid.
PayFieldStyle configures the appearance of the SDK's card fields, including font, text and placeholder colors, background, border, corners, and padding, with separate colors while a field has focus or is invalid. Set it on each field's style. Labels, error text, and the layout around the fields belong to your app. In the drop-in, set PayDropInAppearance.fieldStyle instead.
PayFieldStyle is a struct used on the main actor.
card.number.style = PayFieldStyle(cornerRadius: 12, contentInsets: UIEdgeInsets(top: 14, left: 16, bottom: 14, right: 16))Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
font | UIFont | No | The text and placeholder font. Use a text style font, or one from UIFontMetrics, to follow Dynamic Type. Default: .preferredFont(forTextStyle: .body). |
textColor | UIColor | No | Default: .label. |
placeholderColor | UIColor | No | Default: .placeholderText. |
invalidTextColor | UIColor | No | The text while the field is invalid. Default: .systemRed. |
backgroundColor | UIColor | No | Default: .secondarySystemBackground. |
borderColor | UIColor | No | Default: .separator. |
focusedBorderColor | UIColor | No | The border while the field has focus. Default: .tintColor. |
invalidBorderColor | UIColor | No | The border while the field is invalid, even with focus. Default: .systemRed. |
borderWidth | CGFloat | No | Default: 1. |
cornerRadius | CGFloat | No | Default: 8. |
contentInsets | UIEdgeInsets | No | The padding around the text. Default: 12 on every side. |
Each parameter is also a var, so you can start from PayFieldStyle(), or PayFieldStyle.default, and change a few. A field's height is its font's line height plus the vertical insets, and at least 44 points.
Example
import PayElements
import UIKit
var style = PayFieldStyle()
style.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: UIFont(name: "AvenirNext-Regular", size: 17) ?? .systemFont(ofSize: 17))
style.backgroundColor = .systemBackground
style.borderColor = .systemGray4
style.focusedBorderColor = .systemIndigo
style.cornerRadius = 12
for field in [card.number, card.expiry, card.cvc] as [PayInputField] {
field.style = style
}
card.name?.style = styleNext steps
- Card fields: The fields and their state.
- Style the drop-in: The drop-in's own look.