/// <summary> /// Updates the image. /// </summary> private void UpdateImage() { var iconImage = Element as IconImage; var icon = Iconize.FindIconForKey(iconImage.Icon); if (icon is null) { Control.Layer.Contents = null; return; } if (Element.HeightRequest < 0) { Element.HeightRequest = iconImage.IconSize; } var iconSize = (iconImage.IconSize > 0 ? (nfloat)iconImage.IconSize : (nfloat)Element.HeightRequest); if (iconSize < 0) { throw new ArgumentException("The icon size is under zero !"); } using (var image = icon.ToNSImageWithColor(iconSize, iconImage.IconColor.ToCGColor())) { Control.Layer.Contents = image.CGImage; } }
private void UpdateText() { #if USE_FASTRENDERERS TextChanged -= OnTextChanged; var icon = Iconize.FindIconForKey(Label.Text); if (!(icon is null)) { Text = $"{icon.Character}"; Typeface = Iconize.FindModuleOf(icon).ToTypeface(Context); } TextChanged += OnTextChanged; #else if (!(Control is null)) { Control.TextChanged -= OnTextChanged; var icon = Iconize.FindIconForKey(Label.Text); if (!(icon is null)) { Control.Text = $"{icon.Character}"; Control.Typeface = Iconize.FindModuleOf(icon).ToTypeface(Context); } Control.TextChanged += OnTextChanged; } #endif }
private void UpdateImage() { var icon = Iconize.FindIconForKey(Image.Icon); if (icon == null) { #if USE_FASTRENDERERS SetImageResource(Android.Resource.Color.Transparent); #else Control.SetImageResource(Android.Resource.Color.Transparent); #endif return; } var iconSize = (Image.IconSize == IconImage.AutoSize ? Math.Max(Image.WidthRequest, Image.HeightRequest) : Image.IconSize); var drawable = new IconDrawable(Context, icon).Color(Image.IconColor.ToAndroid()) .SizeDp((Int32)iconSize); #if USE_FASTRENDERERS SetScaleType(Image.IconSize > 0 ? ScaleType.Center : ScaleType.FitCenter); SetImageDrawable(drawable); #else Control.SetScaleType(Image.IconSize > 0 ? ScaleType.Center : ScaleType.FitCenter); Control.SetImageDrawable(drawable); #endif }
/// <summary> /// To the image source. /// </summary> /// <param name="icon">The icon.</param> /// <param name="size">The size.</param> /// <param name="color">The color.</param> /// <returns></returns> public static ImageSource ToImageSource(this IIcon icon, Xamarin.Forms.Color color) { var character = $"{icon.Character}"; var module = Iconize.FindModuleOf(icon); var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), $"/#{module.FontFamily}"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal); if (!typeface.TryGetGlyphTypeface(out var glyphTypeface)) { throw new InvalidOperationException("No glyphtypeface found"); } var glyphIndexes = new UInt16[character.Length]; var advanceWidths = new Double[character.Length]; for (int n = 0; n < character.Length; n++) { ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[character[n]]; glyphIndexes[n] = glyphIndex; double width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0; advanceWidths[n] = width; } var gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes, new System.Windows.Point(0, 0), advanceWidths, null, null, null, null, null, null); var glyphRunDrawing = new GlyphRunDrawing(new SolidColorBrush(ToUIColor(color)), gr); return(new DrawingImage(glyphRunDrawing)); }
/// <summary> /// Updates the text. /// </summary> private void UpdateText() { #if USE_FASTRENDERERS TextChanged -= OnTextChanged; #else Control.TextChanged -= OnTextChanged; #endif var icon = Iconize.FindIconForKey(Button.Text); if (icon != null) { #if USE_FASTRENDERERS Text = $"{icon.Character}"; Typeface = Iconize.FindModuleOf(icon).ToTypeface(Context); #else Control.Text = $"{icon.Character}"; Control.Typeface = Iconize.FindModuleOf(icon).ToTypeface(Context); #endif } #if USE_FASTRENDERERS TextChanged += OnTextChanged; #else Control.TextChanged += OnTextChanged; #endif }
/// <summary> /// Updates the tabbed icons. /// </summary> private void UpdateTabbedIcons(Context context) { var tabLayout = FindViewById <TabLayout>(Iconize.TabLayoutId); if (tabLayout == null || tabLayout.TabCount == 0) { return; } for (var i = 0; i < tabLayout.TabCount; i++) { var tab = tabLayout.GetTabAt(i); if (_icons != null && i < _icons.Count) { var iconKey = _icons[i]; var icon = Iconize.FindIconForKey(iconKey); if (icon == null) { continue; } var drawable = new IconDrawable(context, icon).Color(Color.White.ToAndroid()).SizeDp(20); tab.SetIcon(drawable); } } }
/// <summary> /// Create an <see cref="IconDrawable" />. /// </summary> /// <param name="context">Your activity or application context.</param> /// <param name="icon">The icon you want this drawable to display.</param> public IconDrawable(Context context, IIcon icon) { var module = Iconize.FindModuleOf(icon); Console.WriteLine("IconDrawable.Icon " + icon); if (icon is null) { throw new Java.Lang.IllegalStateException("El icono no puede estar nulo."); } if (module is null) { throw new Java.Lang.IllegalStateException($"Unable to find the module associated with icon {icon.Key}, have you registered the module you are trying to use with Iconize.With(...) in your Application?"); } _context = context; _icon = icon; _paint = new TextPaint { AntiAlias = true, TextAlign = Paint.Align.Center, UnderlineText = false }; _paint.SetStyle(Paint.Style.Fill); _paint.SetTypeface(module.ToTypeface(context)); }
/// <summary> /// Updates the image. /// </summary> private void UpdateImage() { var icon = Iconize.FindIconForKey(Image.Icon); if (!(icon is null)) { Control.Source = icon.ToImageSource(Image.IconColor); } }
/// <summary> /// Updates the text. /// </summary> private void UpdateText() { var icon = Iconize.FindIconForKey(Element.Text); if (!(icon is null)) { Control.Text = $"{icon.Character}"; Control.FontFamily = Iconize.FindModuleOf(icon).ToFontFamily(); } }
/// <summary> /// Updates the text. /// </summary> private void UpdateText() { var icon = Iconize.FindIconForKey(Element.Text); if (icon != null) { Control.Title = $"{icon.Character}"; Control.Font = Iconize.FindModuleOf(icon).ToNSFont((nfloat)Element.FontSize); } }
/// <inheritdoc /> protected override Drawable GetIconDrawable(FileImageSource icon) { var iconize = Iconize.FindIconForKey(icon.File); if (!(iconize is null)) { return(new IconDrawable(Context, icon).SizeDp(20)); } return(base.GetIconDrawable(icon)); }
/// <summary> /// Updates the image. /// </summary> private async Task UpdateImageAsync() { var icon = Iconize.FindIconForKey(Image.Icon); if (!(icon is null)) { var iconSize = (Image.IconSize == IconImage.AutoSize ? Math.Max(Element.WidthRequest, Element.HeightRequest) : Image.IconSize); Control.Source = await icon.ToImageSourceAsync((Int32)iconSize, Image.IconColor); } }
/// <summary> /// Create an <see cref="IconDrawable" />. /// </summary> /// <param name="context">Your activity or application context.</param> /// <param name="iconKey">The icon key you want this drawable to display.</param> /// <exception cref="ArgumentException">If the key doesn't match any icon.</exception> public IconDrawable(Context context, String iconKey) { var icon = Iconize.FindIconForKey(iconKey); if (icon == null) { throw new ArgumentException($"No icon with the key: {iconKey}"); } Init(context, icon); }
/// <inheritdoc /> protected override Task <Tuple <UIImage, UIImage> > GetIcon(Page page) { var icon = Iconize.FindIconForKey(page.Icon.File); if (!(icon is null)) { return(Task.FromResult(Tuple.Create(icon.ToUIImage(25f), (UIImage)null))); } return(base.GetIcon(page)); }
/// <summary> /// Updates the text. /// </summary> private void UpdateText() { var icon = Iconize.FindIconForKey(Element.Text); if (icon != null) { var font = Iconize.FindModuleOf(icon)?.ToUIFont((nfloat)Element.FontSize); if (font != null) { Control.SetTitle($"{icon.Character}", UIControlState.Normal); Control.Font = font; } } }
/// <summary> /// Updates the text. /// </summary> private void UpdateText() { TextChanged -= OnTextChanged; var icon = Iconize.FindIconForKey(Label.Text); if (icon != null) { Text = $"{icon.Character}"; Typeface = Iconize.FindModuleOf(icon).ToTypeface(Context); } TextChanged += OnTextChanged; }
/// <inheritdoc /> protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon) { var iconize = Iconize.FindIconForKey(icon.File); if (!(iconize is null)) { var drawable = new IconDrawable(Context, icon).SizeDp(20); DrawableCompat.SetTintList(drawable, GetItemIconTintColorState()); tab.SetIcon(drawable); return; } base.SetTabIcon(tab, icon); }
/// <summary> /// Updates the text. /// </summary> private void UpdateText() { var icon = Iconize.FindIconForKey(Element.Text); if (!(icon is null)) { var font = Iconize.FindModuleOf(icon)?.ToUIFont((nfloat)Element.FontSize); if (!(font is null)) { Control.Text = $"{icon.Character}"; Control.Font = font; } } }
/// <summary> /// Views the will appear. /// </summary> public override void ViewWillAppear() { base.ViewWillAppear(); for (int i = 0; i < TabView.Items.Length; i++) { var icon = Iconize.FindIconForKey(_icons?[i]); if (icon == null) { continue; } using (var image = icon.ToNSImage(18)) { TabView.Items[i].Image = image; } } }
/// <summary> /// To the UI image. /// </summary> /// <param name="icon">The icon.</param> /// <param name="size">The size.</param> /// <returns></returns> public static UIImage ToUIImage(this IIcon icon, nfloat size) { var attributedString = new NSAttributedString($"{icon.Character}", new CTStringAttributes { Font = new CTFont(Iconize.FindModuleOf(icon).FontName, size), ForegroundColorFromContext = true }); var boundSize = attributedString.GetBoundingRect(new CGSize(10000f, 10000f), NSStringDrawingOptions.UsesLineFragmentOrigin, null).Size; UIGraphics.BeginImageContextWithOptions(boundSize, false, 0f); attributedString.DrawString(new CGRect(0f, 0f, boundSize.Width, boundSize.Height)); using (var image = UIGraphics.GetImageFromCurrentImageContext()) { UIGraphics.EndImageContext(); return(image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)); } }
/// <summary> /// Called prior to the <see cref="P:UIKit.UIViewController.View" /> being added to the view hierarchy. /// </summary> /// <param name="animated">If the appearance will be animated.</param> /// <remarks> /// <para>This method is called prior to the <see cref="T:UIKit.UIView" /> that is this <see cref="T:UIKit.UIViewController" />’s <see cref="P:UIKit.UIViewController.View" /> property being added to the display <see cref="T:UIKit.UIView" /> hierarchy. </para> /// <para>Application developers who override this method must call <c>base.ViewWillAppear()</c> in their overridden method.</para> /// </remarks> public override void ViewWillAppear(Boolean animated) { base.ViewWillAppear(animated); foreach (var tab in TabBar.Items) { var icon = Iconize.FindIconForKey(_icons?[(Int32)tab.Tag]); if (icon == null) { continue; } using (var image = icon.ToUIImage(25f)) { tab.Image = image; tab.SelectedImage = image; } } }
/// <summary> /// To the color of the ns image with. /// </summary> /// <param name="icon">The icon.</param> /// <param name="size">The size.</param> /// <param name="color">The color.</param> /// <returns></returns> public static NSImage ToNSImageWithColor(this IIcon icon, nfloat size, CGColor color) { var attributedString = new NSAttributedString($"{icon.Character}", new CTStringAttributes { Font = new CTFont(Iconize.FindModuleOf(icon).FontName, size), ForegroundColorFromContext = true }); using (var ctx = new CGBitmapContext(IntPtr.Zero, (nint)size, (nint)size, 8, 4 * (nint)(size), CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { ctx.SetFillColor(color); using (var textLine = new CTLine(attributedString)) { textLine.Draw(ctx); } return(new NSImage(ctx.ToImage(), new CGSize(size, size))); } }
/// <summary> /// Updates the button icon. /// </summary> /// <param name="button">The button.</param> private static void UpdateButtonIcon(AppBarButton button) { if (button?.DataContext is IconToolbarItem item) { var icon = Iconize.FindIconForKey(item.Icon); if (icon != null) { button.ClearValue(AppBarButton.IconProperty); button.Icon = new FontIcon { FontFamily = Iconize.FindModuleOf(icon).ToFontFamily(), Glyph = $"{icon.Character}" }; if (item.IconColor != default(Color)) { button.Icon.Foreground = new SolidColorBrush(item.IconColor.ToUIColor()); } button.Visibility = item.IsVisible ? Visibility.Visible : Visibility.Collapsed; } } }
/// <summary> /// Initializes the specified context. /// </summary> /// <param name="context">The context.</param> /// <param name="icon">The icon.</param> /// <exception cref="Java.Lang.IllegalStateException">Unable to find the module associated + /// with icon + icon.Key + , have you registered the module + /// you are trying to use with Iconize.With(...) in your Application?</exception> private void Init(Context context, IIcon icon) { var module = Iconize.FindModuleOf(icon); if (module == null) { throw new Java.Lang.IllegalStateException($"Unable to find the module associated with icon {icon.Key}, have you registered the module you are trying to use with Iconize.With(...) in your Application?"); } _context = context; _icon = icon; _paint = new TextPaint { AntiAlias = true, Color = Android.Graphics.Color.Black, TextAlign = Paint.Align.Center, UnderlineText = false }; _paint.SetStyle(Paint.Style.Fill); _paint.SetTypeface(module.ToTypeface(context)); }
/// <summary> /// To the image source. /// </summary> /// <param name="icon">The icon.</param> /// <param name="size">The size.</param> /// <param name="color">The color.</param> /// <returns></returns> public static async Task <Windows.UI.Xaml.Media.ImageSource> ToImageSourceAsync(this IIcon icon, Int32 size, Color color) { var character = $"{icon.Character}"; var module = Iconize.FindModuleOf(icon); using (var surface = SKSurface.Create(size, size, SKImageInfo.PlatformColorType, SKAlphaType.Premul)) { using (var paint = new SKPaint()) { using (var typeface = SKTypeface.FromFile(Path.Combine(Package.Current.InstalledLocation.Path, module.GetType().GetTypeInfo().Assembly.GetName().Name, module.FontPath))) { paint.Color = color.ToSKColor(); paint.IsAntialias = true; paint.Typeface = typeface; // Adjust TextSize property so text is 90% of size var textWidth = paint.MeasureText(character); paint.TextSize = 0.9f * size * paint.TextSize / textWidth; // Find the text bounds var textBounds = new SKRect(); paint.MeasureText(character, ref textBounds); // Calculate offsets to center the text var xText = (size / 2) - textBounds.MidX; var yText = (size / 2) - textBounds.MidY; // And draw the text surface.Canvas.DrawText(character, xText, yText, paint); } } var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(surface.Snapshot().Encode().AsStream().AsRandomAccessStream()); return(bitmap); } }
/// <summary> /// Updates the image. /// </summary> /// <param name="shouldUpdateImage">if set to <c>true</c> [should update image].</param> private void UpdateImage(Boolean shouldUpdateImage) { if (shouldUpdateImage) { Control.ContentMode = (Image.IconSize == IconImage.AutoSize ? UIViewContentMode.ScaleAspectFit : UIViewContentMode.Center); var icon = Iconize.FindIconForKey(Image.Icon); if (icon == null) { Control.Image = null; return; } var iconSize = (Image.IconSize == IconImage.AutoSize ? Math.Max(Element.WidthRequest, Element.HeightRequest) : Image.IconSize); using (var image = icon.ToUIImage((nfloat)iconSize)) { Control.Image = image; } } Control.TintColor = Image.IconColor.ToUIColor(); }
/// <summary> /// Updates the toolbar items. /// </summary> /// <param name="page">The page.</param> /// <param name="controller">The controller.</param> public static void UpdateToolbarItems(this Page page, UINavigationController controller) { try { if (page == null || controller == null) { return; } if (controller.IsBeingDismissed) { return; } var navController = controller.VisibleViewController; if (navController == null) { return; } if (navController.NavigationItem?.RightBarButtonItems != null) { for (var i = 0; i < navController.NavigationItem.RightBarButtonItems.Length; ++i) { navController.NavigationItem.RightBarButtonItems[i].Dispose(); } } if (navController.ToolbarItems != null) { for (var i = 0; i < navController.ToolbarItems.Length; ++i) { navController.ToolbarItems[i].Dispose(); } } var toolbarItems = page.GetToolbarItems(); if (toolbarItems == null) { return; } List <UIBarButtonItem> primaries = null; List <UIBarButtonItem> secondaries = null; foreach (var toolbarItem in toolbarItems) { var barButtonItem = toolbarItem.ToUIBarButtonItem(toolbarItem.Order == ToolbarItemOrder.Secondary); if (toolbarItem is IconToolbarItem iconItem) { if (!iconItem.IsVisible) { continue; } var icon = Iconize.FindIconForKey(iconItem.Icon); if (icon != null) { using (var image = icon.ToUIImage(22f)) { barButtonItem.Image = image; if (iconItem.IconColor != Color.Default) { barButtonItem.TintColor = iconItem.IconColor.ToUIColor(); } } } } if (toolbarItem.Order == ToolbarItemOrder.Secondary) { (secondaries = secondaries ?? new List <UIBarButtonItem>()).Add(barButtonItem); } else { (primaries = primaries ?? new List <UIBarButtonItem>()).Add(barButtonItem); } } primaries?.Reverse(); navController.NavigationItem.SetRightBarButtonItems(primaries == null ? new UIBarButtonItem[0] : primaries.ToArray(), false); navController.ToolbarItems = (secondaries == null ? new UIBarButtonItem[0] : secondaries.ToArray()); } catch (Exception ex) { Console.WriteLine(ex); } }
/// <summary> /// Create an <see cref="IconDrawable" />. /// </summary> /// <param name="context">Your activity or application context.</param> /// <param name="iconKey">The icon key you want this drawable to display.</param> /// <exception cref="ArgumentException">If the key doesn't match any icon.</exception> public IconDrawable(Context context, String iconKey) : this(context, Iconize.FindIconForKey(iconKey)) { // Intentionally left blank }