How to include multiple fonts into single text - flutter

I have an Arabic text in a pdf file. When I copy the text into Text Widget it becomes weird characters. When I checked the pdf file properties I found that it uses HQPB1, HQPB2, HQPB3, HQPB4 fonts, so I imported all of these fonts to my pubsec.yaml file. The problem is that I can use only one of these 4 fonts at a time but the pdf file uses all of these 4 fonts simultaneously.
this is the original text from pdf
when I added HQPB1.ttf only
when I added HQPB2.ttf only
So I want to include all of these 4 fonts in a Text so that each individual font should be used when it is needed as pdf does.

RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: 'Hello ', style: TextStyle(fontFamily: "Serif", fontSize: 30, color: Colors.black)),
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue, fontSize: 30)),
TextSpan(text: ' world!', style: TextStyle(fontFamily: "Roboto", fontSize: 30, color: Colors.red)),
],
),
)
Output:

In order to have multiple styles on one Text widget you have to use RichText. RichText has a children[] so you can have custom TextStyle (which will use whatever font you want) for each TextSpan
Check it out here -> https://api.flutter.dev/flutter/widgets/RichText-class.html

const Text.rich(
TextSpan(
children: <TextSpan>[
TextSpan(
text: 'Hello ',
style: TextStyle(
fontFamily: "Serif",
fontSize: 30,
color: Colors.black,
),
),
TextSpan(
text: 'bold',
style: TextStyle(
fontFamily: "Arial",
fontWeight: FontWeight.bold,
color: Colors.blue,
fontSize: 30,
),
),
TextSpan(
text: ' world!',
style: TextStyle(
fontFamily: "Roboto",
fontSize: 30,
color: Colors.red,
),
),
],
),
)

Related

Text is shifted vertically in Flutter

I have the following code:
AutoSizeText.rich(
TextSpan(children: [
const TextSpan(
text: "\$",
style: TextStyle(
color: Colors.white,
fontSize: 45,
height: 1.0,
letterSpacing: .0,
fontWeight: FontWeight.w600,
),
),
TextSpan(
text: widget.budget.truncate().toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 90,
),
),
TextSpan(
text: ".${widget.budget.toStringAsFixed(2).split(".")[1]}",
style: const TextStyle(
color: Colors.white,
fontSize: 45,
),
),
]),
style: GoogleFonts.manrope(
fontSize: 45,
letterSpacing: -4.5,
height: 1.0,
fontWeight: FontWeight.w600,
textStyle: const TextStyle(
leadingDistribution: TextLeadingDistribution.even,
)),
textAlign: TextAlign.center,
maxLines: 1,
);
The code results in this (I also tried with Text.rich instead of AutoSizeText but the result is the same):
In the image, I have wrapped the text in a red container to check its bounding box.
I am implementing this element following a design I made in Figma:
Similatly, in this image the red frame has an autolayout applied with all padding set to 0, in a way that simulates wrapping the text in a Container.
The text looks the same in both the design and the Flutter implementation, but the Flutter one is shifted upwards, which on big text sizes is really noticeable. I figured it might have something to do with the vertical alignment of the text but I could not find any way to fix it.
Is this an expected behaviour or am I doing something wrong?

Is it possible to make specific letter in a word to be a custom font in flutter?

I want to make specific letter to have a custom font or style in Flutter, is it possible to do that? I only saw tutorial on how to make specific word in Flutter to have custom style by using RichText. Is there anyway to make a specific letter instead of a word?
for example
𝒜n elephant
In this case the letter A will have a custom font but the letter n does not.
The text parameter in a TextSpan can be omitted. So for what you want, you can just use the children parameter and have the first child be a single character.
RichText(
text: TextSpan(
children: [
TextSpan(
text: 'L',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
const TextSpan(text: 'ike this?')
],
),
),
You can do like this :
Text.rich(
TextSpan(text: 'This is textspan ', children: <InlineSpan>[
TextSpan(
text: 'Widget in flutter',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)
]),
)
You can customize it any way you want :D
String myString = "Helloooo";
String specialChar = "o";
RichText(
text: TextSpan(
children: myString.characters.map((e) {
return TextSpan(
text: e,
style: e == specialChar
? TextStyle(
color: Colors.red, fontWeight: FontWeight.bold, fontSize: 40)
: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold, fontSize: 50),
);
}).toList()));

How to show colored emoji from custom font on Flutter app?

I'd like to use Twemoji font in my Flutter app. I installed this font. But when I try to use it in TextSpan, I see monochromatic emoji instead of colored. How to fix it?
My code:
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: "Some text ", style: GoogleFonts.roboto(
fontSize: 24,
color: Colors.black,
)),
TextSpan(text: "🐣", style: TextStyle(
fontFamily: "TwitterColorEmoji",
fontSize: 24,
)),
]
)
)
I got it, Twemoji font works in all operating system but will currently only show color emoji in Firefox, Thunderbird, Photoshop CC 2017+, and Windows Edge V38.14393+. This is not a limitation of the font, but of the operating systems and applications.
As has been said, die to limits of operation system and applications, this font won't show colored emojies.
I think the best altenative way to show colored emojies is using WidgetSpan with SvgPicture like in code below:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Some text ",
style: GoogleFonts.roboto(
fontSize: 24,
color: Colors.black,
),
),
WidgetSpan(
child: SvgPicture.asset("assets/emojies/confused-face.svg"),
style: TextStyle(fontSize: 24),
),
],
),
),

flutter RichText, TextSpan align

this is my code
RichText(
text: TextSpan(children: [
TextSpan(
text: 'flutterABC',
style: TextStyle(
fontSize: isDesktop(context) ? 64 : 32,
fontWeight: FontWeight.w800)),
TextSpan(
text: 'flutterABC',
style: TextStyle(
fontSize: isDesktop(context) ? 64 : 32,
fontWeight: FontWeight.w800,
color: Colors.indigo))
]),
),
like this. i use RichText and TextSpan.
when i run in chrome, i can't control align
if 'flutterABCflutterABC' is one line > align is center.
but if two line > align is start..
i want to always align center
how can i do?
Try this please
RichText(
text: TextSpan(
children:[
TextSpan(
text: 'flutterABC\n',
style: TextStyle(
fontWeight: FontWeight.w800)),
TextSpan(
text: 'flutterABC',
style: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.indigo))
]),
),
you can use Column to this effect
but if you went to use RichText
you can use \n for newline

How to style an embedded text?

i have a piece of code below and i wanted to style the text "From" differently, i.e, display it black and bold in colour. It should remain in the same format.
Thank you.
Text("From :" + " " + from, style: GoogleFonts.viga(
color: Colors.blue[800],
fontWeight: FontWeight.w500,
), /*TextStyle(color: Colors.blue[800],)*/
),
You can use the RichText and TextSpan classes to achieve this behavior.
RichText(
text: TextSpan(
text: 'From:',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(text: ' $from', style: TextStyle(color: Colors.blue[800])),
],
),
)
You can read more about it here