I have the following XAML code:
<Label Text="Hello Forms with XAML">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>Roboto-Light</OnPlatform.iOS>
<OnPlatform.Android>Roboto-Light.ttf#Roboto-Light</OnPlatform.Android>
<OnPlatform.WinPhone>Assets/Fonts/Roboto-Light.ttf#Roboto-Light</OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
However I get the error "Java.Lang.RuntimeException: Font asset not found Roboto-Light" even though the font is placed in the Assets folder:
What can I do to fix this?
On Android you must ensure that the part after the # is the actual name of the font. This part is NOT directly the same as the file name. I also use Roboto in one of my apps and it's declared as:
<OnPlatform.Android>fonts/Roboto-Regular.ttf#Roboto</OnPlatform.Android>
<OnPlatform.Android>fonts/Roboto-Bold.ttf#Roboto Bold</OnPlatform.Android>
You might want to try yours as:
<OnPlatform.Android>Roboto-Light.ttf#Roboto Light</OnPlatform.Android>
You can find out this name on Mac by selecting the font in FontBook and checking its Full Name property.
You can find a comprehensive guide to using custom fonts in Xamarin.Forms at this Xamarin Developers Guide Link.
As Steven highlighted, it's important to ensure that your font ttf file is in the right place, in this instance you appear to have got it right, the location for the android project is indeed the 'assets' folder.
Xamarin.Forms for Android can reference a custom font that has been
added to the project by following a specific naming standard. First
add the font file to the Assets folder in the application project and
set Build Action: AndroidAsset.
Setting a custom font in C# backing classes, snippet amended from the above source:
public class SomeMethod()
{
new Label
{
Text = "Hello, Forms!",
FontFamily = Device.OnPlatform(null, "Roboto-Light.ttf#Roboto-Light", null)
}
}
The xaml should actually be:
<Label Text="Hello Forms with XAML">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android">Roboto-Light.ttf#Roboto-Light</On>
</OnPlatform>
</Label.FontFamily>
</Label>
It is worth mentioning that the roboto theme should be part of the native android project already if your targeting API 14 or above. So on android you can actually just use the 'sans-serif-light' font which is infact Roboto. Follow this link to see an excellent answer on an android specific thread about the fonts they now include in API 14+
Related
I'm new to Xamarin development, but try to be exact.
Using Visual Studio 2022, with Xamarin version 17.0.0.182 (as displayed in VS About).
Found this great article on how to use Bootstrap icons, but it seems to be out of date. When I followed the steps and provided hex codes for the characters, I didn't get the expected glyiphs.
I found that
new Xamarin needs new methods,
new Bootstrap fonts need new ways.
Here they are.
1.) Get the font from the Bootstrap github repo at https://github.com/twbs/icons
Go to releases (https://github.com/twbs/icons/releases), choose latest, scroll down to Assets, download the bootstrap-icons-x.x.x.xip file.
Unzip it, and find fonts/bootstrap-icons.woff.
Not something you can use in Xamarin right away :-(
2.) Convert the woff file to ttf
I googled for a converter and used https://cloudconvert.com/woff-to-ttf
Now you have the ttf you need :-)
3.) Now follow the current Xamarin method of adding a font to your app.
The process is described here: https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/
In short:
add the ttf file to the shared Xamarin project, Embedded Resources / Fonts
change file properties / Build Action to "Embedded resource"
register the font by adding a line to the end of Assemblyinfo.cs:
[assembly: ExportFont("bootstrap-icons.ttf", Alias = "Bootstrap")]
4.) Use it in xaml like
<Label FontFamily="Bootstrap" Text=""/>
to display a magnifying glass.
You may also define a Label style, but I'll skip that for now.
4/b.) To use it in xaml via binding
Oh, if things were easy...
When using a binding to display a glyph, there's one more hoop to jump, thanks to these guys for the solution: Using data binding, how do I bind text which contains emojis to a label and have it display correctly?
So, in xaml:
<!-- set BindingContext to MyViewModel -->
<Label FontFamily="Bootstrap" Text="{Binding StateIconName}" />
in your MyViewModel:
public string StateIconName
{
get => WebUtility.HtmlDecode("");
}
5.) To browse available glyphs (icons)
Open the overview page: https://icons.getbootstrap.com/
6.) To find the unicode character code, also referred to as "Unicode HTML Entity"
Check the name of the icon above in step 5.
Open the Bootstrap-icons.css file from Github: https://github.com/twbs/icons/blob/main/font/bootstrap-icons.css
Search for the name of the glyph to find the character code, something like:
.bi-zoom-in::before { content: "\f62c"; }
Use this code in xaml like
I have a Xamarin.Forms shared project. On Android all images work fine. I wanted to add them to my iOS project, but they don't show. I've added them to Assets.xcassets:
{
"filename": "logo_launcher.png",
"scale": "1x",
"idiom": "universal"
}
And I use the following XAML:
<Image Source="logo_launcher.png" x:Name="logo" />
I've added all images except for the vector ones. I'm testing it on a iPhone 8 - 11.4 simulator.
Although it is deprecated, I tried to add the images to the "Resource" folder. But I couldn't even select the .png files. So that didn't work either.
I'm lost at what to do. Is there some option I need to enable, or something to include in the XAML to make it work?
Just select all images in resources folder in ios and right click-->build action-->bundle resource. It will work.
Check/Reproduce some steps below:
Check the build action on your image resource, in shared PCL it should be Embedded Resource, in Android the build action should be AndroidResourceand BundleResource in iOS
Try to set Copy to output directory to Always copy
Delete bin/obj folders, switch build configuration from debug to release, rebuild project and then back to debug
Try to delete the image from iOS, rebuild, re-add again, and then repeat the steps above
Also, you can try to set the ImageSource as you can see below:
<Image x:Name="logo">
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<On Platform="Android" Value="logo_launcher.png"/>
<On Platform="iOS" Value="logo_launcher"/>
</OnPlatform>
</Image.Source>
</Image>
I hope this can help you.
Maybe I found a possible solution using Assets.xcassets, check the steps below:
Main.xaml
<Image x:Name="iconBrain">
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS><FileImageSource File="IconBrain"/></OnPlatform.iOS>
<OnPlatform.Android><FileImageSource File="ic_brain"/></OnPlatform.Android>
</OnPlatform>
</Image.Source>
</Image>
Android: Add the images on the respective Drawable folders under Resourses directory.
iOS: Create a new Image Set under Assets.xcassets and add the Images.
Done. No extra properties, configs, or xamarin tricks.
Check out my GitHub for the full solution, also I tried to reproduce the same environment w/ Xamarin.Forms, Shared Project and iPhone 8 - 11.4 simulator.
Another point, you can't add the images under mipmap directories, because the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders as before.
It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density. - Google blogpost
Finally, thank you for the information about the deprecated approach by Apple.
Prior to iOS 9, images were typically placed in the Resources folder with Build Action: BundleResource. However, this method of working with images in an iOS app has been deprecated by Apple. For more information, see Image Sizes and Filenames. - Images in Xamarin.Forms
I hope this can help you.
I tried solutions from the following links but none worked.
where-to-put-images-for-xamarin-froms-application
developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Local_Images
image-not-showing-in-xamarin-forms-app.html
I used below syntax
<Image Source="lock.png" HeightRequest="20" WidthRequest="20" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="0" Grid.Column="0"></Image>
Here is the image for where i've kept image
No image displays
Edit
These are the only things in my XAML page.
In my case the issue was of assembly.
when trying all the possible solutions like
1.) Changing Build Action
2.) Adding Image to all the folders within Resources
3.) Naming Convention should not include - in File/Image Name.
4.) Changing Background Color
I also got one solution while searching for embedded Images that assembly was missing here.
This change worked for me in using Local Images.
Got that solution from this link
Right click the lock.png image and see if there is an option "include in project"
If there is, project cannot find image
In my case I need to use method Forms.Init(Context activity, Bundle bundle), in my SplashScreen OnCreate method and Activity class OnCreate method. But it also important that images build action properties is set on AndroidResource.
I have a Master Detail page and I want to set the icon in the navigation bar to show the menu. Therefore I put my images into an asset catalog (Asset Catalogs > Media > menu). If I want to reference that image from XAML it doesn't show up, when the image is in the asset catalog.
I tried different paths, but they all don't work. Debugging also doesn't show any images. It seems that Xamarin.Forms doesn't support asset catalogs, but that is not true. On a normal content page I have this:
<ContentPage.ToolbarItems>
<ToolbarItem Text="Configuration" Activated="OpenConfiguration" Order="Primary" Priority="0">
<ToolbarItem.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<OnPlatform.iOS>gear.png</OnPlatform.iOS>
<OnPlatform.Android>gear.xml</OnPlatform.Android>
<OnPlatform.WinPhone>Assets/gear.png</OnPlatform.WinPhone>
</OnPlatform>
</ToolbarItem.Icon>
</ToolbarItem>
</ContentPage.ToolbarItems>
This is working without problems. On my MasterPage (page, which is responsible for showing the menu entries in the master detail template) I have this:
<ContentPage.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<OnPlatform.iOS>menu.png</OnPlatform.iOS>
<OnPlatform.Android>menu.xml</OnPlatform.Android>
<OnPlatform.WinPhone>Assets/menu.png</OnPlatform.WinPhone>
</OnPlatform>
</ContentPage.Icon>
Bascially, it is the same code, but the icon doesn't show up. I get the icon only displaying if I put the images into Resources folder ...
But how can I use the images from the asset catalog in my XAML?
The only response I got from Xamarin is, that FileImageSource can't be used with an asset catalog. That isn't completely true, but since it doesn't work every time one shouldn't use it.
I am trying to add some Images as Image Sets in a Xamarin.Forms project. I followed this guide . But the icon is not displayed. Is it possible actually to use Image Sets in a Xamarin Forms Solution for the iOS project?
<Image VerticalOptions="Center"
HorizontalOptions="End"
Source="check_outline_500_active.png"
WidthRequest="40">
</Image>
Yes you can.
create the Asset catalog
add Image Set and name it "check_outline_500_active" (without .png)
add the images
done
my xaml
<Image VerticalOptions="Center"
HorizontalOptions="Center"
Source="check_outline_500_active.png">
</Image>
Result
Note
You might have to delete mtbs (on the mac) and bin/obj (on your project) or the build cache can fool you :)
Try to add images in resources folder under xamarin ios project and rename file like :
check_outline_500_active.png,
check_outline_500_active#2x.png,
check_outline_500_active#3x.png
but in code use : check_outline_500_active.png