Beispiel #1
0
 bool TryGetTextAppearance(int appearance, out double val)
 {
     val = 0;
     try
     {
         using (var value = new TypedValue())
         {
             _applicationContext ??= global::Android.App.Application.Context.ApplicationContext;
             if (_applicationContext.Theme.ResolveAttribute(appearance, value, true))
             {
                 var       textSizeAttr        = new[] { AndroidResource.Attribute.TextSize };
                 const int indexOfAttrTextSize = 0;
                 using (TypedArray array = _applicationContext.ObtainStyledAttributes(value.Data, textSizeAttr))
                 {
                     val = _applicationContext.FromPixels(array.GetDimensionPixelSize(indexOfAttrTextSize, -1));
                     return(true);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Application.Current?.FindMauiContext()?.CreateLogger <FontNamedSizeService>()?
         .LogWarning(ex, "Error retrieving text appearance");
     }
     return(false);
 }
Beispiel #2
0
        public Style GetStyle(int style)
        {
            var result = new Style(typeof(Label));

            double fontSize   = 0;
            string fontFamily = null;

            global::Android.Graphics.Color defaultColor = global::Android.Graphics.Color.Argb(0, 0, 0, 0);
            global::Android.Graphics.Color androidColor = defaultColor;

            _applicationContext ??= global::Android.App.Application.Context.ApplicationContext;
            using (var value = new TypedValue())
            {
                if (_applicationContext.Theme.ResolveAttribute(style, value, true))
                {
                    var styleattrs = new[] { global::Android.Resource.Attribute.TextSize, global::Android.Resource.Attribute.FontFamily, global::Android.Resource.Attribute.TextColor };
                    using (TypedArray array = _applicationContext.ObtainStyledAttributes(value.ResourceId, styleattrs))
                    {
                        fontSize     = _applicationContext.FromPixels(array.GetDimensionPixelSize(0, -1));
                        fontFamily   = array.GetString(1);
                        androidColor = array.GetColor(2, defaultColor);
                    }
                }
            }

            if (fontSize > 0)
            {
                result.Setters.Add(new Setter {
                    Property = Label.FontSizeProperty, Value = fontSize
                });
            }

            if (!string.IsNullOrEmpty(fontFamily))
            {
                result.Setters.Add(new Setter {
                    Property = Label.FontFamilyProperty, Value = fontFamily
                });
            }

            if (androidColor != defaultColor)
            {
                result.Setters.Add(new Setter {
                    Property = Label.TextColorProperty, Value = Color.FromRgba(androidColor.R, androidColor.G, androidColor.B, androidColor.A)
                });
            }

            return(result);
        }