void UpdateFont()
        {
            if (_elementFontFamilyProperty != null)
            {
                var fontFamilyName = _elementFontFamilyProperty.GetValue(Element) as string;
                var assembly       = _embeddedResourceFontEffect?.Assembly;
                if (assembly == null && fontFamilyName != null)
                {
                    assembly = AssemblyExtensions.AssemblyFromResourceId(fontFamilyName);
                }
                if (assembly != null && !Settings.AssembliesToInclude.Contains(assembly))
                {
                    Settings.AssembliesToInclude.Add(assembly);
                }
                var uwpFontFamilyName = FontService.ReconcileFontFamily(fontFamilyName);
                if (uwpFontFamilyName == null)
                {
                    Console.WriteLine("WARNING EmbeddedResourceFontEffect: Could not find ResourceId [" + fontFamilyName + "] in visible assemblies.");
                }
                //DebugMessage("uwpFontFamily=[" + uwpFontFamilyName + "] Length=[" + uwpFontFamilyName.Length + "]");

                var fontFamily = new FontFamily(uwpFontFamilyName);
                if (_controlFontFamilyProperty != null)
                {
                    _controlFontFamilyProperty.SetValue(Control, fontFamily);
                }
                else if (Control is Windows.UI.Xaml.Controls.TextBlock textBlock)
                {
                    textBlock.FontFamily = fontFamily;
                }
                else if (Control is Windows.UI.Xaml.Controls.TextBox textBox)
                {
                    textBox.FontFamily = fontFamily;
                }
                else if (Control is Windows.UI.Xaml.Controls.Button button)
                {
                    button.FontFamily = fontFamily;
                }
                else if (Control is Windows.UI.Xaml.Controls.ComboBox comboBox)
                {
                    comboBox.FontFamily = fontFamily;
                }
                else if (Control is Xamarin.Forms.Platform.UWP.FormsComboBox formsComboBox)
                {
                    formsComboBox.FontFamily = fontFamily;
                }
                else if (Control is Windows.UI.Xaml.Controls.DatePicker datePicker)
                {
                    datePicker.FontFamily = fontFamily;
                }
                else if (Control is Xamarin.Forms.Platform.UWP.FormsTextBox formsTextBox)
                {
                    formsTextBox.FontFamily = fontFamily;
                }
                else
                {
                    Console.WriteLine("WARNING EmbeddedResourceFontEffect: Could not find FontFamily property for native element of type [" + Control.GetType() + "]");
                }
            }
        }
Exemple #2
0
        void UpdateFont()
        {
            if (_elementFontFamilyProperty != null && _controlFontFamilyProperty != null)
            {
                var fontFamilyName = _elementFontFamilyProperty.GetValue(Element) as string;
                var assembly       = _embeddedResourceFontEffect?.Assembly;
                if (assembly != null && !Settings.AssembliesToInclude.Contains(assembly))
                {
                    Settings.AssembliesToInclude.Add(assembly);
                }
                var uwpFontFamilyName = FontService.ReconcileFontFamily(fontFamilyName);
                DebugMessage("uwpFontFamily=[" + uwpFontFamilyName + "] Length=[" + uwpFontFamilyName.Length + "]");

                var fontFamily = new FontFamily(uwpFontFamilyName);
                _controlFontFamilyProperty.SetValue(Control, fontFamily);
            }
        }
        static void AddInline(TextBlock textBlock, Forms9Patch.Label label, MetaFont metaFont, string text, int startIndex, int length)
        {
            var run = new Run();

            run.Text       = text.Substring(startIndex, length);
            run.FontSize   = metaFont.Size;
            run.FontWeight = metaFont.Bold ? Windows.UI.Text.FontWeights.Bold : Windows.UI.Text.FontWeights.Normal;
            run.FontStyle  = metaFont.Italic ? Windows.UI.Text.FontStyle.Italic : Windows.UI.Text.FontStyle.Normal;


            if (TextDecorationsPresent && metaFont.Strikethrough)
            {
                ApplyTextDecorations(run, Decoration.Strikethrough);
            }

            switch (metaFont.Baseline)
            {
            case FontBaseline.Numerator:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Superscript);
                break;

            case FontBaseline.Superscript:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Superscript);
                break;

            case FontBaseline.Denominator:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Subscript);
                break;

            case FontBaseline.Subscript:
                run.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                Typography.SetVariants(run, Windows.UI.Xaml.FontVariants.Subscript);
                break;

            default:
                if (metaFont.Family != null)
                {
                    run.FontFamily = new Windows.UI.Xaml.Media.FontFamily(FontService.ReconcileFontFamily(metaFont.Family));
                }
                break;
            }

            if (!metaFont.BackgroundColor.IsDefaultOrTransparent())
            {
                try
                {
                    textBlock.ApplyBackgroundColor(metaFont.BackgroundColor, startIndex, length);
                }
                catch (Exception)
                {
                    //throw new Exception("It appears that this Xamarin.Forms.UWP app was built with a Windows TargetVersion < 10.0.16299.0 (Windows 10 Fall Creators Update).  10.0.16299.0 is needed to support Forms9Patch.Label.HtmlText background color attributes.", e);
                }
            }

            if (metaFont.IsActionEmpty())
            {
                run.Foreground = new Windows.UI.Xaml.Media.SolidColorBrush(metaFont.TextColor.ToWindowsColor());
                if (TextDecorationsPresent && metaFont.Underline)
                {
                    ApplyTextDecorations(run, Decoration.Underline);
                }
                textBlock.Inlines.Add(run);
            }
            else
            {
                run.Foreground = new Windows.UI.Xaml.Media.SolidColorBrush(Xamarin.Forms.Color.Blue.ToWindowsColor());
                if (TextDecorationsPresent)
                {
                    ApplyTextDecorations(run, Decoration.Underline);
                }
                var hyperlink = new Hyperlink();
                hyperlink.Inlines.Add(run);
                hyperlink.Click += (Hyperlink sender, HyperlinkClickEventArgs args) => label.Tap(metaFont.Action.Id, metaFont.Action.Href);
                textBlock.Inlines.Add(hyperlink);
            }
        }