Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Layered font image source #9934

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 167 additions & 1 deletion Xamarin.Forms.Controls/GalleryPages/FontImageSourceGallery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xamarin.Forms.Controls.Issues;
using System.Collections.Generic;
using Xamarin.Forms.Controls.Issues;

namespace Xamarin.Forms.Controls
{
Expand Down Expand Up @@ -39,19 +40,183 @@ public FontImageSourceGallery()
{
Source = new FontImageSource
{
Color = Color.White,
Glyph = Ionicons[Ionicons.Length - 1].ToString(),
FontFamily = fontFamily,
Size = 20
},
});

var i = 1;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
Color = Color.White,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf2d1'.ToString(),
FontFamily = fontFamily,
Size = 20
},
new FontImageSource
{
Glyph = '\uf100'.ToString(),
FontFamily = fontFamily,
Color = Color.Yellow,
Size = 20
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
Size = 30,
Color = Color.White,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf2d1'.ToString(),
FontFamily = fontFamily,
Size = 20
},
new FontImageSource
{
Glyph = '\uf100'.ToString(),
FontFamily = fontFamily,
Color = Color.Yellow,
Size = 20
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
Size = 20,
Color = Color.White,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf2d1'.ToString(),
FontFamily = fontFamily,
Size = 30
},
new FontImageSource
{
Glyph = '\uf100'.ToString(),
FontFamily = fontFamily,
Color = Color.Yellow,
Size = 30
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
Size = 30,
Color = Color.White,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf2d1'.ToString(),
FontFamily = fontFamily,
Size = 30
},
new FontImageSource
{
Glyph = '\uf100'.ToString(),
FontFamily = fontFamily,
Color = Color.Orange,
Size = 20
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
FontFamily = fontFamily,
Size = 20,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf23e'.ToString(),
Color = Color.White,
},
new FontImageSource
{
Glyph = '\uf23f'.ToString(),
Color = Color.CornflowerBlue,
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
FontFamily = fontFamily,
Size = 20,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf24c'.ToString(),
Color = Color.White,
},
new FontImageSource
{
Glyph = '\uf24d'.ToString(),
Color = Color.Red,
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

foreach (char c in Ionicons)
{
grid.Children.Add(new Image
{
Source = new FontImageSource
{
Color = Color.White,
Glyph = c.ToString(),
FontFamily = fontFamily,
Size = 20
Expand All @@ -62,6 +227,7 @@ public FontImageSourceGallery()
}, i % 10, i / 10);
i++;
}

Content = new ScrollView
{
VerticalOptions = LayoutOptions.FillAndExpand,
Expand Down
82 changes: 82 additions & 0 deletions Xamarin.Forms.Core/LayeredFontImageSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Xamarin.Forms
{
[ContentProperty(nameof(Layers))]
public class LayeredFontImageSource : ImageSource
{
public override bool IsEmpty => Layers == null || Layers.Count == 0 || Layers.All(x => x.IsEmpty);

public static readonly BindableProperty LayersProperty = BindableProperty.Create(nameof(Layers), typeof(IList<FontImageSource>), typeof(LayeredFontImageSource), null,
propertyChanged: (b, o, n) => ((LayeredFontImageSource)b).OnLayersChanged((IList<FontImageSource>)o, (IList<FontImageSource>)n));

public IList<FontImageSource> Layers
{
get => (IList<FontImageSource>)GetValue(LayersProperty);
set => SetValue(LayersProperty, value);
}

public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(Color), typeof(LayeredFontImageSource), default(Color),
propertyChanged: (b, o, n) => ((LayeredFontImageSource)b).OnSourceChanged());

public Color Color
{
get => (Color)GetValue(ColorProperty);
set => SetValue(ColorProperty, value);
}

public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(LayeredFontImageSource), default(string),
propertyChanged: (b, o, n) => ((LayeredFontImageSource)b).OnSourceChanged());

public string FontFamily
{
get => (string)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}

public static readonly BindableProperty GlyphProperty = BindableProperty.Create(nameof(Glyph), typeof(string), typeof(LayeredFontImageSource), default(string),
propertyChanged: (b, o, n) => ((LayeredFontImageSource)b).OnSourceChanged());

public string Glyph
{
get => (string)GetValue(GlyphProperty);
set => SetValue(GlyphProperty, value);
}

public static readonly BindableProperty SizeProperty = BindableProperty.Create(nameof(Size), typeof(double), typeof(LayeredFontImageSource), 30d,
propertyChanged: (b, o, n) => ((LayeredFontImageSource)b).OnSourceChanged());

[TypeConverter(typeof(FontSizeConverter))]
public double Size
{
get => (double)GetValue(SizeProperty);
set => SetValue(SizeProperty, value);
}

void OnLayersChanged(IList<FontImageSource> oldLayers, IList<FontImageSource> newLayers)
{
if (oldLayers != null)
{
foreach (var layer in oldLayers)
{
layer.SourceChanged -= LayerSourceChanged;
}
}
if (newLayers != null)
{
foreach (var layer in newLayers)
{
layer.SourceChanged += LayerSourceChanged;
}
}
OnSourceChanged();
}

void LayerSourceChanged(object sender, EventArgs e)
{
OnSourceChanged();
}
}
}
1 change: 1 addition & 0 deletions Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))]
[assembly: ExportImageSourceHandler(typeof(FontImageSource), typeof(FontImageSourceHandler))]
[assembly: ExportImageSourceHandler(typeof(LayeredFontImageSource), typeof(FontImageSourceHandler))]
[assembly: Xamarin.Forms.Dependency(typeof(Deserializer))]
[assembly: Xamarin.Forms.Dependency(typeof(ResourcesProvider))]
[assembly: Preserve]
Expand Down
Loading