зеркало из https://github.com/DeGsoft/maui-linux.git
Android Custom Font support (#236)
* Implemented basic Support for Android Custom fonts * Fixed Tabs * Removed Private * Changed behaviour to use UWP FontFamily style names * Fixed Bug
This commit is contained in:
Родитель
a490740a2e
Коммит
378ecc2a1a
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using Android.Graphics;
|
using Android.Graphics;
|
||||||
|
using AApplication = Android.App.Application;
|
||||||
|
|
||||||
namespace Xamarin.Forms.Platform.Android
|
namespace Xamarin.Forms.Platform.Android
|
||||||
{
|
{
|
||||||
|
@ -8,6 +10,8 @@ namespace Xamarin.Forms.Platform.Android
|
||||||
{
|
{
|
||||||
static readonly Dictionary<Tuple<string, FontAttributes>, Typeface> Typefaces = new Dictionary<Tuple<string, FontAttributes>, Typeface>();
|
static readonly Dictionary<Tuple<string, FontAttributes>, Typeface> Typefaces = new Dictionary<Tuple<string, FontAttributes>, Typeface>();
|
||||||
|
|
||||||
|
static readonly Regex LoadFromAssets = new Regex(@"\w+\.((ttf)|(otf))\#\w*");
|
||||||
|
|
||||||
static Typeface s_defaultTypeface;
|
static Typeface s_defaultTypeface;
|
||||||
|
|
||||||
public static float ToScaledPixel(this Font self)
|
public static float ToScaledPixel(this Font self)
|
||||||
|
@ -21,11 +25,14 @@ namespace Xamarin.Forms.Platform.Android
|
||||||
{
|
{
|
||||||
case NamedSize.Micro:
|
case NamedSize.Micro:
|
||||||
return 10;
|
return 10;
|
||||||
|
|
||||||
case NamedSize.Small:
|
case NamedSize.Small:
|
||||||
return 12;
|
return 12;
|
||||||
|
|
||||||
case NamedSize.Default:
|
case NamedSize.Default:
|
||||||
case NamedSize.Medium:
|
case NamedSize.Medium:
|
||||||
return 14;
|
return 14;
|
||||||
|
|
||||||
case NamedSize.Large:
|
case NamedSize.Large:
|
||||||
return 18;
|
return 18;
|
||||||
}
|
}
|
||||||
|
@ -44,21 +51,21 @@ namespace Xamarin.Forms.Platform.Android
|
||||||
if (Typefaces.TryGetValue(key, out result))
|
if (Typefaces.TryGetValue(key, out result))
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
var style = TypefaceStyle.Normal;
|
if (self.FontFamily == null)
|
||||||
if ((self.FontAttributes & (FontAttributes.Bold | FontAttributes.Italic)) == (FontAttributes.Bold | FontAttributes.Italic))
|
{
|
||||||
style = TypefaceStyle.BoldItalic;
|
var style = ToTypefaceStyle(self.FontAttributes);
|
||||||
else if ((self.FontAttributes & FontAttributes.Bold) != 0)
|
|
||||||
style = TypefaceStyle.Bold;
|
|
||||||
else if ((self.FontAttributes & FontAttributes.Italic) != 0)
|
|
||||||
style = TypefaceStyle.Italic;
|
|
||||||
|
|
||||||
if (self.FontFamily != null)
|
|
||||||
result = Typeface.Create(self.FontFamily, style);
|
|
||||||
else
|
|
||||||
result = Typeface.Create(Typeface.Default, style);
|
result = Typeface.Create(Typeface.Default, style);
|
||||||
|
}
|
||||||
Typefaces[key] = result;
|
else if (LoadFromAssets.IsMatch(self.FontFamily))
|
||||||
return result;
|
{
|
||||||
|
result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(self.FontFamily));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var style = ToTypefaceStyle(self.FontAttributes);
|
||||||
|
result = Typeface.Create(self.FontFamily, style);
|
||||||
|
}
|
||||||
|
return (Typefaces[key] = result);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static bool IsDefault(this IFontElement self)
|
internal static bool IsDefault(this IFontElement self)
|
||||||
|
@ -68,26 +75,50 @@ namespace Xamarin.Forms.Platform.Android
|
||||||
|
|
||||||
internal static Typeface ToTypeface(this IFontElement self)
|
internal static Typeface ToTypeface(this IFontElement self)
|
||||||
{
|
{
|
||||||
|
if (self.IsDefault())
|
||||||
|
return s_defaultTypeface ?? (s_defaultTypeface = Typeface.Default);
|
||||||
|
|
||||||
var key = new Tuple<string, FontAttributes>(self.FontFamily, self.FontAttributes);
|
var key = new Tuple<string, FontAttributes>(self.FontFamily, self.FontAttributes);
|
||||||
Typeface result;
|
Typeface result;
|
||||||
if (Typefaces.TryGetValue(key, out result))
|
if (Typefaces.TryGetValue(key, out result))
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
var style = TypefaceStyle.Normal;
|
if (self.FontFamily == null)
|
||||||
if ((self.FontAttributes & (FontAttributes.Bold | FontAttributes.Italic)) == (FontAttributes.Bold | FontAttributes.Italic))
|
{
|
||||||
style = TypefaceStyle.BoldItalic;
|
var style = ToTypefaceStyle(self.FontAttributes);
|
||||||
else if ((self.FontAttributes & FontAttributes.Bold) != 0)
|
|
||||||
style = TypefaceStyle.Bold;
|
|
||||||
else if ((self.FontAttributes & FontAttributes.Italic) != 0)
|
|
||||||
style = TypefaceStyle.Italic;
|
|
||||||
|
|
||||||
if (self.FontFamily != null)
|
|
||||||
result = Typeface.Create(self.FontFamily, style);
|
|
||||||
else
|
|
||||||
result = Typeface.Create(Typeface.Default, style);
|
result = Typeface.Create(Typeface.Default, style);
|
||||||
|
}
|
||||||
|
else if (LoadFromAssets.IsMatch(self.FontFamily))
|
||||||
|
{
|
||||||
|
result = Typeface.CreateFromAsset(AApplication.Context.Assets, FontNameToFontFile(self.FontFamily));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var style = ToTypefaceStyle(self.FontAttributes);
|
||||||
|
result = Typeface.Create(self.FontFamily, style);
|
||||||
|
}
|
||||||
|
return (Typefaces[key] = result);
|
||||||
|
}
|
||||||
|
|
||||||
Typefaces[key] = result;
|
public static TypefaceStyle ToTypefaceStyle(FontAttributes attrs)
|
||||||
return result;
|
{
|
||||||
|
var style = TypefaceStyle.Normal;
|
||||||
|
if ((attrs & (FontAttributes.Bold | FontAttributes.Italic)) == (FontAttributes.Bold | FontAttributes.Italic))
|
||||||
|
style = TypefaceStyle.BoldItalic;
|
||||||
|
else if ((attrs & FontAttributes.Bold) != 0)
|
||||||
|
style = TypefaceStyle.Bold;
|
||||||
|
else if ((attrs & FontAttributes.Italic) != 0)
|
||||||
|
style = TypefaceStyle.Italic;
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
static string FontNameToFontFile(string fontFamily)
|
||||||
|
{
|
||||||
|
int hashtagIndex = fontFamily.IndexOf('#');
|
||||||
|
if (hashtagIndex >= 0)
|
||||||
|
return fontFamily.Substring(0, hashtagIndex);
|
||||||
|
|
||||||
|
throw new InvalidOperationException($"Can't parse the {nameof(fontFamily)} {fontFamily}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Загрузка…
Ссылка в новой задаче