/// <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 }
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 }
/// <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> /// 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 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); } }
/// <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) { 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; }
/// <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> /// 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> /// 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); } }