I using custom font for my Flutter app:
return MaterialApp (
theme: ThemeData(
fontFamily: "MyFont"
)
)
but in one place of application I need to use "system font". this is especially important for iOS (Sf Pro). If I understand correctly, this is the system font for iOS. But how can I use "system font" if I already use "custom font" at application level?
I tried:
final fontFamily = DefaultTextStyle.of(context).style!.fontFamily;
But this code return "MyFont".
also I tried importing from apple site. but there is only one big SF-Pro.ttf(8Mb) file inside the image. Not - SFPro-Regular.ttf, SFPro-SemiBold.ttf ... Flutter does not support .otf files.
How to solve this problem? any advice I will be grateful
Related
I'm developing an app in Flutter for iOS where the user can easily look-up a word using the built-in dictionaries.
I used SelectableText for text so that user could select a word and that toolbar options for the selected text could be displayed. Among the ToolbarOptions of SelectableText I see copy, cut, paste and selectAll but not lookUp nor share.
I didn't try yet, but I know that share is available through a flutter package called "share". How do I add a lookUp to the SelectableText's toolbar menu? Is there any package that I do not aware of or I should dive into native for this?
My current code:
SelectableText(
this.deck[index % this.deck.length].front,
style: kCardTextStyle,
toolbarOptions: ToolbarOptions(copy: true, /* share: ??? */))
I wanted to use a custom font in my react-native project, but I got an error on ios simulator
How can I solve it
I tried every way but I couldn't find a solution.
Unrecognized Font Family: sfproregular
Error: Error Screenshot
Project Structure:
Project structure screenshot
react-native.config.js
module.exports = {
project: {
ios: {},
android: {}, // grouped into "project"
},
assets: ["./assets/fonts/"], // stays the same
};
versions
"react": "16.13.1",
"react-native": "0.63.2",
"react-native-gesture-handler": "^1.7.0",
"react-native-phone-input": "^0.2.4",
"react-navigation": "^4.4.0"
I wanted to use the font in "Welcome.js"
Welcome.js
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
Image,
StatusBar,
TouchableOpacity,
Button,
ImageBackground,
} from 'react-native';
import Logo from '../components/WelcomeLogo';
import arkaplan from '../assets/images/arkaplan.png';
export default class Welcome extends React.Component {
render() {
return (
<>
<ImageBackground source={arkaplan} style={styles.constrain} >
<StatusBar barStyle="light-content"/>
<Logo />
<Text style={styles.welcome} >Welcome!</Text>
</View>
</>
)
}
};
const styles = StyleSheet.create({
constrain: {
flex: 1,
alignItems:'center',
justifyContent:'center',
welcome: {
fontSize: 50,
color: 'white',
fontFamily: 'sfprogregular',
marginTop: 20,
marginBottom: 5,
},
});
Much like you I experienced this Unrecognized Font family issue when running the iOS build via Terminal. For whatever reason, it doesn't effect the final archive build or if one runs the build via Xcode. This is on the latest RN 0.63.3 in macOS Catalina 10.15.7 with Xcode 12.0.1.
The issue was with the font name itself.
Solution Summary
In your case, I recommend the following steps to take.
Install your font sfprogregular on your macOS system
With the font installed, and selected in Font Book press CMD+I to see postscript name
Rename the font files in your project to their respective PostScript names
Run npx react-native link in your project to setup the renamed fonts
Do cleanup of the older font files prior to the rename (remove old fonts)
Change the name of the font to the postscript name in code when referring to the font in styles
My experience detailed
In my case, we are using TradeGothic.
Initially, when we linked the font, the name of the font file itself was:
Trade Gothic LT.ttf
Android is fine with this, but not iOS. That's because Android relies on the filename, but iOS depends on the PostScript name of the font.
To fix this, I renamed the ttf file to it's postscript name TradeGothicLT.ttf. For the bold one which was Trade Gothic LT Bold.ttf I renamed it to TradeGothicLT-Bold.ttf.
I then did npx react-native link inside my project to connect my newly renamed fonts. Then, I cleaned up by removing the older .ttf files from the XCode project under resources (just press delete on each of the red font files that are no longer there) and removed the font files from the older Android link process in the folder /android/app/src/main/assets/fonts/. In the info.plist file, under the section UIAppFonts remove the older font filenames.
Finally in code, instead of referring to 'Trade Gothic LT' we now refer to it as 'TradeGothicLT'. That's it, it works now!
You can find the postscript name of the font by installing the .ttf file (double click it) on macOS and in Font Book with the font selected press CMD+I to get information about the font.
NOTE: It's not necessary to install the font to macOS, it's just the only way I know of to get the PostScript name. If you already have the PostScript name of the fonts, you don't need to install them.
I hope this helps you and others that might stumble upon this issue!
After adding the custom fonts, you need to link it using react-native link.
That will create an entry in Info.plist file (iOS) & android/src/main/assets/fonts/ directory (Android).
If the above command fails, you need to add those fonts manually in the android directory & plist file.
Sometimes your font name is not the same for Android and iOS (since iOS uses postscript name as fontFamily). In most cases, in android, the font is the same as the file, for example, I had this problem when using HelveticaNeueLTPro-BdEx font in iOS.
I am sharing the fix that helps me out.
Run npx react-native link. Maybe you already did.
Add this inside the didFinishLaunchingWithOptions method on your AppDelegate.m:
for (NSString *familyName in [UIFont familyNames]){
NSLog(#"Family name: %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"--Font name: %#", fontName);
}
}
Then, when you launch your app with xcode, you will be able to see all the fonts installed on your app, so you just have to use it the way it is listed on your console:
You can add fontFamily based on OS like:
Platform.OS == 'android' ? 'Linotype - HelveticaNeueLTPro-BlkEx' : 'HelveticaNeueLTPro-BlkEx'
Or you can rename the font file name and then run npx react-native link again and also maybe have to delete the previous UIAppFonts entry for the previously linked font in Info.plist file.
I tried adding an image with a SVG source to my flutter application.
new AssetImage("assets/images/candle.svg"))
But I didn't get any visual feedback.
How can I render an SVG picture in Flutter?
Fonts are a great option for a lot of cases.
I've been working on a library to render SVGs on a canvas, available here:
https://github.com/dnfield/flutter_svg
The API as of right now would look something like
new SvgPicture.asset('asset_name.svg')
to render asset_name.svg (sized to its parent, e.g. a SizedBox). You can also specify a color and blendMode for tinting the asset..
It's now available on pub and works with a minimum of Flutter version 0.3.6. It handles a bunch of cases but not everything - see the GitHub repo for updates and to file issues.
The original GitHub issue referenced by Colin Jackson is really not meant to be primarily focused on SVG in Flutter. I opened another issue here for that: https://github.com/flutter/flutter/issues/15501
Flutter does not currently support SVG. Follow issue 1831 for updates.
If you absolutely need vector drawing you can see the Flutter Logo widget as an example of how to draw using the Canvas API, or rasterize your image on the native side and pass it to Flutter as a bitmap, but for now your best bet is probably to embed high-resolution rasterized asset images.
Developers from the Flutter community created a lib to handle svg files. We can use it as
new SvgPicture.asset(
'assets/images/candle.svg',
height: 20.0,
width: 20.0,
allowDrawingOutsideViewBox: true,
),
I found a small example of SVG implementation here.
The work around for now is use fonts
https://icomoon.io/
fonts:
- family: icomoon
fonts:
- asset: assets/fonts/icomoon.ttf
Useage
static const IconData TabBarHome= const IconData(0xe906, fontFamily: 'icomoon');
static const IconData TabBarExplore= const IconData(0xe902, fontFamily: 'icomoon');
Replace the ### eg (906)
You can follow the below steps
- visit http://fluttericon.com
- upload your SVG icons
- click on download button
- you will get two files
1. iconname.dart 2. iconname.ttf font file
- use this file in flutter & import iconname.dart
step 1. add dependency in your pubspec.yaml
dependencies:
flutter_svg: any
the Advantage of version any is you can use in any SDK version
but dependencies version is most important so add suitable version for your dependency.
step 2. import the flutter svg package in your app
import 'package:flutter_svg/flutter_svg.dart';
step 3. just use like below
SvgPicture.asset(
'assets/images/bm-icon1.svg',
width: 18.0,
height: 18.0,
),
You can use this library for rendering SVG Images - https://pub.dev/packages/flutter_svg
Example -
Container(
child: SvgPicture.asset("assets/images/yourImage.svg")
)
I solved similar problem by converting the svg images to ttf at fluttericon.com.
These are the steps involved.
Keep all your svg files in a folder.
Upload all of them to fluttericon by using the uploader there.
You can select all of them and drag and drop at the spot.
Give your icons a dart class name at the top: like, Svgicons
Select all your icons and press Download.
It will download a zip file with a dart file and fonts folder which contains Svgicons.ttf.
copy the fonts folder to your project folder and refer to it as an asset in pubspec.yaml.
fonts:
- family: Svgicons
fonts:
- asset: fonts/Svgicons.ttf
import the svgicons_icons.dart where you are going to use the images.
Sample:
import '../svgicons_icons.dart';
import 'social_media.dart';
class SocialMediaRepository {
List<SocialMedia> getSocialMedia = [
SocialMedia(id: 1, title: "Facebook", iconAsset:Svgicons.facebook, isFavorite: false),
SocialMedia(id: 2, title: "Instagram", iconAsset: Svgicons.instagram, isFavorite: false),
SocialMedia(id: 3, title: "LinkedIn", iconAsset: Svgicons.linkedin, isFavorite: false),
....
Here iconAsset is of type IconData which used to be a String referring to the svg image originally.
Display the ttf by using
...
Icon(item.iconAsset),
SizedBox(width: 10),
Text(item.title),
],
),
where item is the object containing IconData.
Now you can remove the reference to svg images from the project.
You can use flare to create animations and just import .flr as an asset
import 'package:flare_flutter/flare_actor.dart';
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new FlareActor("assets/Filip.flr", alignment:Alignment.center, fit:BoxFit.contain, animation:"idle");
}
}
visit flare_flutter https://pub.dev/packages/flare_flutter
Of possible interest: There's a newer Flutter SVG library called jovial_svg. I wrote it to handle a more robust subset of SVG than what I was able to find on pub.dev at the time. While I was at it, I also made a binary format that SVG assets can be compiled to. It's a little more compact, and about 10x faster to read, so it's a good choice for assets you package with your application.
The library is at https://pub.dev/packages/jovial_svg
flutter support special type of svg
if have style tag then not load svg image in flutter
flutter support inline styling in svg
Svg in flutter https://pub.dev/packages/flutter_svg
add package in pubspec.yaml
dependencies:
flutter_svg: any
assets:
-assets/images/
to insert svg.
SvgPicture.asset('assets/images/password-icon.svg',
width: 24, height: 29.2),
i'm currently using react native ART module, and everything is ok except the Text component, but the bug is only happening in iOS, in android everything seems to work as expected.
When i try to use the Text component in iOS react trows an this error:
Image Output Error in App
And in Xcode console output's this:
2016-09-20 17:17:17.663 [error][tid:main][RCTConvert.m:57] Error setting property 'frame' of ARTText with tag #7: JSON value '' of type NSNull cannot be converted to NSDictionary
Since react native ART doesn't have documentation, it's very possible that I'm not doing something right, but I already read the source code and nothing is working that I try.
Additional Information:
React Native version:0.33.0
Platform: iOS
Operating System: macOS
Xcode: 8.0
RnPlay is down so i'm gonna post the code here:
https://jpst.it/NAac
As soon as the RNplay is up, i will update this post and put the code there.
Was having this error myself too, turns out that you need to provide the font property, according to this issue
Copied from there
<ARTText font={`13px "Helvetica Neue", "Helvetica", Arial`} fill="#000000" x={16} y={16}>Hello</ARTText>
You can specify font values this way, which works on both iOS and Android:
<ART.Text
font={{
fontFamily: 'Helvetica Neue',
fontSize: 13,
}}
fill="#000000"
x={16}
y={16}
>
Hello
</ARTText>
Other attributes, as discerned from the source code, are:
fontWeight: (number | 'bold' | 'normal')
fontStyle: ('italic' | 'normal')
I'm currently loading two fonts from a qrc file. One of them is .ttf and the other is .otf. When I test my build on Android, they load and work fine (although the loading time is fairly long..)
However when I test it on my iPhone and/or simulator, none of the fonts load at all. They just seem to default back to Arial or something. Furthermore, only the .otf file works fine on Windows. The .ttf file defaults back to Arial and the characters are offset by 2 characters (so the word "ok" would be "qm")
What the hell is going on?
The fonts are located in:
"qrc:/fnt/res/fnt/MuseoSans_500.otf"
"qrc:/fnt/res/fnt/museo100-regular.ttf"
and I'm using this to load the fonts:
FontLoader {
id: museosans500
source: "qrc:/fnt/res/fnt/MuseoSans_500.otf"
}
FontLoader {
id: museo100
source: "qrc:/fnt/res/fnt/museo100-regular.ttf"
}
When accessing the fonts I set the font.family to museosans500.name and museo100.name. Like I said, they work fine on Android. But neither work on iOS, and only one of them works on Windows.
I'm using Qt Creator 3.4.0 and Qt 5.4.1.
Any help is much appreciated!
So I didn't solve it for Windows but this is an application for iOS and Android.
Basically I loaded the fonts through main.cpp instead with:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// Load custom fonts
if(QFontDatabase::addApplicationFont(QStringLiteral(":/fnt/res/fnt/MuseoSans_500.otf")) == -1)
qDebug() << "Failed to load font Museo Sans 500";
if(QFontDatabase::addApplicationFont(QStringLiteral(":/fnt/res/fnt/museo100-regular.ttf")) == -1)
qDebug() << "Failed to load font Museo 100";
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/res/qml/LoadScreen.qml")));
return app.exec();
}
Originally this didn't actually work. But that's because I was setting the font.family to be "Museo Sans 500" and "Museo 100" since that is what Windows would print in console when I asked for a full list of available font families.
When I printed out the font families list in Android, Mac and iOS I actually got "Museo Sans" and "Museo" respectively. Changing the font.family to those strings worked fine.
Unfortunately I have found no solution for Windows yet, but like I've said that doesn't matter to me.