Using google fonts I have no issues with getting font weights to display correctly. But as soon as I use a custom font I have trouble. I'm using the sofia pro font and I've put the fonts into my assets folder and set up by pubspc.yaml as following:
fonts:
- family: SofiaPro
fonts:
- asset: assets/fonts/SofiaProUltraLight.ttf
weight: 200
- asset: assets/fonts/SofiaProLight.ttf
weight: 300
- asset: assets/fonts/SofiaProRegular.ttf
weight: 400
- asset: assets/fonts/SofiaProMedium.ttf
weight: 500
- asset: assets/fonts/SofiaProSemiBold.ttf
weight: 600
- asset: assets/fonts/SofiaProBold.ttf
weight: 700
- asset: assets/fonts/SofiaProBlack.ttf
weight: 900
- family: SofiaProRegular
fonts:
- asset: assets/fonts/SofiaProRegular.ttf
weight: 400
I'm then referencing the font in two ways - once where it's in its own font family with no other weights and once where it's in with a list of other weights.
Container(
height: height,
child: Center(
child: Text(
"This text has weight w400",
style: TextStyle(fontFamily: 'SofiaPro', fontSize: size, fontWeight: FontWeight.w400),
)),
),
Container(
height: height,
child: Center(
child: Text(
"This text has weight w400",
style: TextStyle(fontFamily: 'SofiaProRegular', fontSize: size, fontWeight: FontWeight.w400),
)),
),
So, the only difference is the font family I'm referring to, but the text displayed is only shown correctly for the one where it's in a family by itself. For the other one I get the weight of the first font in the list. Not sure what I'm missing here.
Probably there's no font family SofiaProRegular at all in the fonts that you provide.
Please note that you're adding assets/fonts/SofiaProRegular.ttf twice both for SofiaPro and SofiaProRegular family.
If you want to use the same font in two font families try to copy the font file under a different name.
Related
Im trying to setup my font here: https://github.com/kaspernordkvist/uncut_sans/tree/main/Static
this is my pubspec.yaml
fonts:
- family: UncutSans
fonts:
- asset: lib/theme/fonts/UncutSans-Bold.ttf
weight: 700
- asset: lib/theme/fonts/UncutSans-Semibold.ttf
weight: 600
- asset: lib/theme/fonts/UncutSans-Light.ttf
weight: 300
- asset: lib/theme/fonts/UncutSans-Regular.ttf
weight: 400
- asset: lib/theme/fonts/UncutSans-Medium.ttf
weight: 500
In my ios emulator everything just looks fine, but on my real device it completely ignores my font weights I just set up. For example:
...
TextStyle(
fontWeight: FontWeight.w300,
fontSize: 18.0,
fontFamily: 'UncutSans',
color: Theme.colorDarkBodyText,)
...
This Text is thin and looks fine on simulator. On my real device it more looks like a font weight of Semibold 600.
Whats missing here?
EDIT: Folder Structure (Screenshot I made when I tried to add the variable type of the font. It doesnt make any difference using the variable font and update pubspec.yaml or choose the single ttfs.)
Ok, so I'm trying to implement a custom icon in flutter but it's not rendering the image.
enter image description here
Font code:
fonts:
family: icon
fonts:
asset: assets/fonts/MyFlutterApp.ttf
Icon code:
icon: new Icon(const IconData(0xe800, fontFamily: 'icon'))
I'm using this font https://www.dafont.com/pixeloid-sans.font in flutter, but can't accept custom colors, like this:
pubspec.yaml :
fonts:
- family: Pixeloid Sans
fonts:
- asset: fonts/PixeloidSans-Bold.ttf
- asset: fonts/PixeloidSans.ttf
now, code:
Text(
'You have pushed the button this many times:',
style: Theme.of(context).textTheme.headline5!.apply(color: Colors.amberAccent, fontFamily: "Pixeloid Sans"),
)
The font changes, but color not.
I am trying to put a specified unicode character into a Text widget, but only if there is a glyph available in the fontFamily font specified in the TextStyle.
What currently happens by design is the fontFamilyFallback font is checked for a glyph, and if not found then the system font is checked, then if still no glyph found a 'not found' style glyph is rendered instead - usually a box with an X inside (depends on system I think).
I wonder if there is a way to disable that fall-back or even better have a list of available glyphs before building the text widget?
Attached some example code, and the results of the code in a screenshot. The code uses two fonts available via google fonts, and attempts to output the euro unicode character \u20ac. You can see from the screenshot that Syne has a glyph at u20ac, but Arvo does not. This could also be validated in for example Windows Character map.
Flutter Code (nothing in texttheme other than fontsize):
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'No fontfamily, no fontfamilyfallback \u20AC',
),
Text(
'fontfamily Syne Tactile: \u20AC',
style: TextStyle(fontFamily: 'Syne'),
),
Text(
'fontfamily Arvo, no fontfamilyfallback \u20AC',
style: TextStyle(fontFamily: 'Arvo'),
),
Text(
'fontfamily Arvo, fontfamilyfallback: Syne Tactile \u20AC',
style:
TextStyle(fontFamily: 'Arvo', fontFamilyFallback: ['Syne']),
),
],
),
),
); }
pubspec.yaml:
fonts:
- family: Arvo
fonts:
- asset: assets/Arvo-Regular.ttf
- family: Syne
fonts:
- asset: assets/SyneTactile-Regular.ttf
Flutter Web Output
Found a bit of a hack/workaround. If I load the AdobeBlank font into the package, and use that as fontFamilyFallback, then a glyph with zero width is displayed. Not ideal (better to know before building), but sort of does the job.
Can also then try and determine width before building using something like this.
Hopefully there is a better answer out there still, as I'd much rather be able to load a font and know what glyphs are available somehow.
I want to add an icon with text. I didn't find any clear answer. Please suggest to me how to do this in a flutter. Just like this one.
Here are two approaches, depending on exactly what you want.
RichText with WidgetSpan
Using RichText you can mix TextSpans with WidgetSpans. WidgetSpan allows you to place any Flutter widget inline with the text. For example:
RichText(
text: TextSpan(
style: Theme.of(context).textTheme.body1,
children: [
TextSpan(text: 'Created with '),
WidgetSpan(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: Icon(Icons.airport_shuttle),
),
),
TextSpan(text: 'By Michael'),
],
),
)
Will give the following:
(Note: MaterialIcons did not include a heart when I answered, but does now with Icons.favorite)
This is a good general purpose solution, but for your specific example there's something simpler...
Emoji
Flutter's text supports Emoji out of the box. So you can do this:
Center(
child: Text(
'Created with ❤ ️by Michael',
maxLines: 1,
),
),
And you get this:
You can also use Unicode escapes in the string and get the same result:
'Created with \u2764️ by Michael'
Note that not all fonts have support for the full set of Emoji, so make sure you test on a variety of devices or use a specific font (see other answer below).
Regarding Emojis, in android not all emojis are supported (depending on OS version).
For full emoji compatibility you can use the google free font Noto Color Emoji at https://www.google.com/get/noto/#emoji-zsye-color
Add it to the fonts folder
add in pubspec.yaml
fonts:
- family: NotoEmoji
fonts:
- asset: fonts/NotoColorEmoji.ttf
weight: 400
use with TextStyle
Text("🤑", TextStyle(fontFamily: 'NotoEmoji'))