native_flutter_fonts 1.0.0 native_flutter_fonts: ^1.0.0 copied to clipboard
A Flutter plugin to automatically register Flutter fonts on the native side of Android and iOS.
native_flutter_fonts #
Currently iOS only. Android coming soon.
Provides a font registry and resolver on the native side so that native code can resolve and use Flutter fonts.
Automatically attempts to load all fonts from the Flutter font asset manifest, and stores them in a singleton instance so that they can be accessed.
This is currently used by the following plugins:
- native_tab_bar
- accessible_text_view
Installation / Setup #
- Add as a dependency to your plugin's or project's pubspec.yaml.
- Important! Add to your
plugin_name.podspec
(plugin), orPodfile
(app project).
Plugin plugin_name.podspec
#
Pod::Spec.new do |s|
...
s.dependency 'Flutter'
s.dependency 'native_flutter_fonts' # <-- add here
...
end
App Podfile
-- Do not add if your app already uses a plugin that depends on the native_flutter_fonts
pod! #
...
target 'Runner' do
use_frameworks!
use_modular_headers!
...
pod 'native_flutter_fonts' # <-- add here
...
end
...
Usage #
In your Swift file:
import native_flutter_fonts
...
let textFont: UIFont? = FlutterFontRegistry.resolve(family: 'Roboto', size: 14, weight: 400)
let fallbackFont: UIFont = FlutterFontRegistry.resolveOrSystemDefault(family: 'My Font', size: 14, weight: 400)
The resolve
and resolveOrSystemDefault
functions expect font weights in Flutter FontWeight
units. These range from 100 (thin) to 400 (normal) to 900 (extra bold), in increments of 100.
However, iOS font weights (CGFloat
) range from -1.0 (thin) to 0.0 (normal) to +1.0 (extra bold).
To convert between the two, we provide two convenience functions:
FlutterFontRegistry.flutterWeightFromAppleWeight(_ weight: CGFloat) -> Int
FlutterFontRegistry.appleWeightFromFlutterWeight(_ weight: Int) -> CGFloat