I want to specify a monospace font in a Text widget:
Text("I would like this to be monospace.")
How can I do that? I don't care about the actual font so long as it is monospace because this is for a developer-only screen.
As per the official docs, you should:
Import the font files.
Declare the font in the pubspec.
Set a font as the default.
Use a font in a specific widget.
Now I will put these steps from the document here, and you can read the entire document if you want more details:
Step 1:
After you have your fonts ready (ttf for example), you add them to your project directory under the assets folder, for example (from the link above):
awesome_app/
fonts/
Raleway-Regular.ttf
Raleway-Italic.ttf
RobotoMono-Regular.ttf
RobotoMono-Bold.ttf
Step 2:
Declare the font in pubspec.yaml, example:
flutter:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
- family: RobotoMono
fonts:
- asset: fonts/RobotoMono-Regular.ttf
- asset: fonts/RobotoMono-Bold.ttf
weight: 700
Step 3:
You have two options for how to apply fonts to text: as the default font or only within specific widgets.
In your case, you want to use the font in a specific widget, so you do:
child: Text(
'Roboto Mono sample',
style: TextStyle(fontFamily: 'RobotoMono'),
),
Related
Can I ask how to add a font to flutter consisting of two words, do I put _ or put a space or write the two words next to each other?
I mean like 'Arima' it's a font from one word so when I add it to pubspec.yaml I will write -family: Arima so if I have a font like 'Cormorant SC' what should I write in pubspec.yaml -family: Cormorant_Sc or -family: CormorantSc or what ?
Try to write
CormorantSC
in your pubspec.yaml file
Both will work.. if you notice the example in pubspec file you will also see a font name with space
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
I am using a fontfamily Montserrat which doesn't support the superscript letter
For Example,
XVIIIᵉ theword is sent from the api
When I try to use this I get like this
Text(theWord,fontFamily: 'Monserrat')
I'm actually trying to clone an Insta page. How do I remove the space between two texts inside a column in Flutter pointed with a green arrow in the screenshot? The upper text "6,667" had to be bold so I had to separate it with "Posts".
Please see the screenshot here:
for line spacing use height property in TextStyle
Text('Hello World', style: TextStyle(height: 1.0 // change this as you want
))
Remove the \n in \nPosts Text Widget
The issue is in your 2nd Text("\n Posts");
Please, remove the \n from the text and it will be okay
I have encountered some issues for setting up SF Mono font in VS Code.
Method 1
Add the following configuration in settings.json
"editor.fontFamily": "SF Mono"
The font seems to be bold, and changing "editor.fontWeight" to 100 doesn't help.
Method 2
"editor.fontFamily": "SFMono-Regular"
The texts have regular font weights now, but the comment is still bold.
Method 3
Try to configure the comment text separately.
"editor.fontFamily": "SFMono-Regular",
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "comment",
"settings": {
"fontStyle": "normal"
}
}
]
}
It seems configuring comment texts to normal gives them regular font weight.
My question is how to have regular SF Mono weights while having italic comments?
To use SF Mono in VS Code:
Remove your local version of SF Mono if it appears in Font Book.
Download the most recent version of SF Mono from Apple here: https://developer.apple.com/fonts/
In VS Code's settings set SF Mono as your preferred font. My config is SF Mono, Menlo, Monaco, 'Courier New', monospace
Restart VS Code
SF Mono should display properly, with the correct weight italics.
I want to set the selected label to appear bold without implementing the font family-bold version.
Ex:-
the default way to set bold is as follows
UIFont *myFont = [UIFont fontWithName:#"Helvetica-BoldOblique"
size:[UIFont systemFontSize]];
But i want to change the font weight(i.e BOld, Italic or Underline) only. without calling the "fontWithName".
Thanks in advance
You are out of luck. For non-system fonts, you have to use fontWithName to get to the bold version. From the documentation of UIFont:
fontName
The fully specified name of the font. This name incorporates both the font family name and the specific style information for the font.
Possible workaround:
So, while you cannot have a generic method that would switch every font to bold, you could still implement this by maintaining a set of string constants that you can look up to switch to the correct bold version of the fonts you are using in your app.
I recently wrote a blog post about that topic.
You can see it at http://eclipsesource.com/blogs/2012/07/26/uifont-from-a-css-definition/
With the code I provide there, you can get your desired UIFont as easy as:
UIFont *myFont = [FontResolver fontWithDescription:#"font-family: Helvetica; font-weight: bold; font-style: italic;"];
If you want the system font (Helvetica Neue), you can use boldSystemFontOfSize:. Otherwise you're stuck specifying bold in the font name. See the UIFont reference for details.
labelName.font=[UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
Use this statement it will help you