Skip to content

Commit

Permalink
Add support for fontFamily on iOS (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesblasco authored Jul 25, 2021
1 parent e50d4a5 commit 9804530
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Binary file added example/fonts/OtomanopeeOne.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions example/lib/screens/themes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class _ThemeCardExampleState extends State<ThemeCardExample> {
child: CardField(
autofocus: true,
enablePostalCode: postalCodeEnabled,
style: TextStyle(fontFamily: 'OtomanopeeOne'),
onCardChanged: (_) {},
decoration: InputDecoration(
labelText: theme.inputDecorationTheme.floatingLabelBehavior ==
Expand Down
6 changes: 5 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ flutter:
uses-material-design: true

assets:
- assets/
- assets/
fonts:
- family: OtomanopeeOne
fonts:
- asset: fonts/OtomanopeeOne.ttf
32 changes: 29 additions & 3 deletions packages/stripe_ios/ios/Classes/CardFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,39 @@ class CardFieldView: NSObject, FlutterPlatformView, STPPaymentCardTextFieldDeleg
if let textErrorColor = cardStyle["textErrorColor"] as? String {
cardField.textErrorColor = UIColor(hexString: textErrorColor)
}
if let fontSize = cardStyle["fontSize"] as? Int {
cardField.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
}

if let placeholderColor = cardStyle["placeholderColor"] as? String {
cardField.placeholderColor = UIColor(hexString: placeholderColor)
}

let fontSize = CGFloat(cardStyle["fontSize"] as? Int ?? 18);
if let fontFamily = cardStyle["fontFamily"] as? String {
// Register font
registerFont(fontFamily)
let font = UIFont(name: "\(fontFamily)", size: fontSize)
cardField.font = font ?? UIFont.systemFont(ofSize: fontSize)
} else {
cardField.font = UIFont.systemFont(ofSize: fontSize)
}
}
}

func registerFont(_ fontFamily: String) {
let controller = UIApplication.shared.delegate?.window??.rootViewController as? FlutterViewController;
let bundle = Bundle.main
let fontKey = controller?.lookupKey(forAsset: "fonts/\(fontFamily).ttf")
let path = bundle.path(forResource: fontKey, ofType: nil)
guard let fontData = NSData(contentsOfFile: path ?? "") else {
return;
}
guard let dataProvider = CGDataProvider(data: fontData) else {
return;
}
let fontRef = CGFont(dataProvider)
var errorRef: Unmanaged<CFError>? = nil
if let fr = fontRef {
CTFontManagerRegisterGraphicsFont(fr, &errorRef)

}
}

Expand Down

0 comments on commit 9804530

Please sign in to comment.