-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Xamarin.Forms.FontIconSource Spec #3189
Description
Font Icons
The goal of this spec is to support the use of font icons (e.g., the Segoe MDL 2 icons or Font Awesome) as image sources.
Note: Nothing in this specification is guaranteed to be final; all features, implementations, and interfaces are subject to change.
API
IFontElement
Implementing classes support setting various font properties.
Note: this is not a new interface; it's already part of Forms. It is included here only for reference purposes.
public interface IFontElement
{
FontAttributes FontAttributes { get; }
string FontFamily { get; }
double FontSize { get; }
}
FontIconSource
Supports using glyph icons.
public class FontIconSource : ImageSource, IFontElement
{
public static readonly BindableProperty GlyphProperty;
public string Glyph { get; set; }
public static readonly BindableProperty ColorProperty;
public Color Color { get; set; }
public static readonly BindableProperty NameProperty;
public string Name { get; set; }
}
Properties
API | Description |
---|---|
Glyph | Gets or sets the charactor code to be displayed. |
Color | Gets or sets the color of the glyph. |
Name | Gets or sets the 'friendly' name of the glyph. If this property is set, the value of Glyph will be retrieved from the FontIconRegistry . |
LayeredFontIconSource
Supports layering glyph icons (e.g., see: https://docs.microsoft.com/en-us/windows/uwp/design/style/segoe-ui-symbol-font#layering-and-mirroring).
[ContentProperty("Layers")]
public class LayeredFontIconSource : ImageSource, IFontElement
{
public IList<FontIconSource> Layers { get; }
}
Properties
API | Description |
---|---|
Layers | Gets the set of icons to be displayed. The icons are stacked in the order they are defined, last one on top. |
If a FontIconSource
in the Layers
list does not specify a font property, that property's value is inherited from the LayeredFontIconSource
.
FontIconRegistry
Supports registering 'friendly' names for icon glyphs. A Forms application has a single instance of this class.
public class FontIconRegistry
{
public void SetDefaultIconFontFamily(string fontFamily);
public void Clear();
public void SetGlyphName(string fontFamily, string glyph, string name);
public void SetGlyphName(string glyph, string name);
public string GetGlyph(string name);
public string GetGlyph(string fontFamily, string name);
}
Methods
API | Description |
---|---|
SetDefaultIconFontFamily | Sets the default font family to use when registering and resolving glyphs. If this value is set, it will be used by any FontIconSource which does not specify a FontFamily . |
Clear | Removes all entries from the regsitry. |
SetGlyphName(string fontFamily, string glyph, string name) | Registers a friendly name for a glyph with a specific font family. |
SetGlyphName(string glyph, string name) | Registers a friendly name for a glyph with the default font family. Throws an exception if a default font family is not set. |
GetGlyph(string name) | Retrieves the glyph for the specified name using the default font family. Throws an exception if the default font family is not set or the name is not found. |
GetGlyph(string name) | Retrieves the glyph for the specified name and font family. Throws an exception if either the font family or name are not found. |
FontIconExtension
Supports specifying a FontIconSource
inline in an Image
tag's Source
property. For example,
<Image Source="{FontIcon Name=share,Color={StaticResource primaryColor}}" />
public sealed class FontIconExtension : IMarkupExtension<ImageSource>
{
...
}