public Task <UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default, float scale = 1) { UIImage image = null; if (imagesource is IconImageSource iconsource && FontRegistry.HasFont(iconsource.Name, out var font)) { // This will allow lookup from the Embedded Fonts var glyph = font.GetGlyph(iconsource.Name); var cleansedname = FontExtensions.CleanseFontName(font.Alias); var uifont = UIFont.FromName(cleansedname ?? string.Empty, (float)iconsource.Size) ?? UIFont.SystemFontOfSize((float)iconsource.Size); var iconcolor = iconsource.Color.IsDefault ? _defaultColor : iconsource.Color; var attString = new NSAttributedString(glyph, font: uifont, foregroundColor: iconcolor.ToUIColor()); var imagesize = ((NSString)glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _)); UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f); var ctx = new NSStringDrawingContext(); var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx); attString.DrawString(new RectangleF( (imagesize.Width / 2) - (boundingRect.Size.Width / 2), (imagesize.Height / 2) - (boundingRect.Size.Height / 2), imagesize.Width, imagesize.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); if (image != null && iconcolor != _defaultColor) { image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); } } return(Task.FromResult(image)); }
public Task <UIImage> LoadImageAsync( ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1f) { UIImage image = null; var fontsource = imagesource as FontImageSource; if (fontsource != null) { // This will allow lookup from the Embedded Fonts var font = Font.OfSize(fontsource.FontFamily, fontsource.Size).ToUIFont(); var iconcolor = fontsource.Color ?? _defaultColor; var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToUIColor()); var imagesize = ((NSString)fontsource.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _)); UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f); var ctx = new NSStringDrawingContext(); var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx); attString.DrawString(new RectangleF( imagesize.Width / 2 - boundingRect.Size.Width / 2, imagesize.Height / 2 - boundingRect.Size.Height / 2, imagesize.Width, imagesize.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); if (image != null && iconcolor != _defaultColor) { image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); } } return(Task.FromResult(image)); }
internal UIImage RenderImage(IFontImageSource imageSource, float scale) { var font = FontManager.GetFont(imageSource.Font); var color = (imageSource.Color ?? Colors.White).ToPlatform(); var glyph = (NSString)imageSource.Glyph; var attString = new NSAttributedString(glyph, font, color); var imagesize = glyph.GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _)); UIGraphics.BeginImageContextWithOptions(imagesize, false, scale); var ctx = new NSStringDrawingContext(); var boundingRect = attString.GetBoundingRect(imagesize, 0, ctx); attString.DrawString(new CGRect( imagesize.Width / 2 - boundingRect.Size.Width / 2, imagesize.Height / 2 - boundingRect.Size.Height / 2, imagesize.Width, imagesize.Height)); var image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)); }
public Task <UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default, float scale = 1) { UIImage image = null; if (imagesource is IconImageSource iconsource && IconFontRegistry.Instance.TryFindIconForKey(iconsource.Name, out var icon)) { var iconcolor = iconsource.Color.IsDefault ? _defaultColor : iconsource.Color; var imagesize = new SizeF((float)iconsource.Size, (float)iconsource.Size); var font = UIFont.FromName(icon.FontFamily ?? string.Empty, (float)iconsource.Size) ?? UIFont.SystemFontOfSize((float)iconsource.Size); UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f); var attString = new NSAttributedString(icon.Glyph, font: font, foregroundColor: iconcolor.ToUIColor()); var ctx = new NSStringDrawingContext(); var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx); attString.DrawString(new RectangleF( (imagesize.Width / 2) - (boundingRect.Size.Width / 2), (imagesize.Height / 2) - (boundingRect.Size.Height / 2), imagesize.Width, imagesize.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); if (iconcolor != _defaultColor) { image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); } } return(Task.FromResult(image)); }
public Task <UIImage> LoadImageAsync( ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1f) { UIImage image = null; var fontsource = imagesource as FontImageSource; if (fontsource != null) { var iconcolor = fontsource.Color != Color.Default ? fontsource.Color : Color.White; var imagesize = new SizeF((float)fontsource.Size, (float)fontsource.Size); var font = UIFont.FromName(fontsource.FontFamily ?? string.Empty, (float)fontsource.Size) ?? UIFont.SystemFontOfSize((float)fontsource.Size); UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f); var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToUIColor()); var ctx = new NSStringDrawingContext(); var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx); attString.DrawString(new RectangleF( imagesize.Width / 2 - boundingRect.Size.Width / 2, imagesize.Height / 2 - boundingRect.Size.Height / 2, imagesize.Width, imagesize.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); if (iconcolor != Color.Default) { image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); } } return(Task.FromResult(image)); }
public static CGRect GetBoundingRect (this NSString This, CGSize size, NSStringDrawingOptions options, UIStringAttributes attributes, NSStringDrawingContext context) { // Define attributes var attr = new NSMutableDictionary (); attr.Add (NSFont.NameAttribute, attributes.Font.NSFont); var rect = This.BoundingRectWithSize (size, options, attr); // HACK: Cheating on the height return new CGRect(rect.Left, rect.Top , rect.Width, rect.Height * 1.5f); }
public static int resizeUILabelHeight(string text, UIFont font, nfloat width) { var nsText = new NSMutableAttributedString(text); nsText.AddAttribute(UIStringAttributeKey.Font, font, new NSRange(0, nsText.Length)); var ctxt = new NSStringDrawingContext(); var expectedSize = nsText.GetBoundingRect(new CGSize(width, float.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, ctxt).Size; int expectedHeight = (int)Math.Round(expectedSize.Height); return(expectedHeight); }
/* * Render the page here: we assume we are already in a normalized coordinate system which maps * our standard aspect ratio (3:4) to (1:1) * The reason why we do this is to reuse the same drawing code for both the preview and the * full screen; for full screen rendering, we map the whole view, whereas the preview maps * the whole preview image to a quarter of the page. * */ public RectangleF [] RenderPage(Page page, SizeF size, bool unstyledDrawing) { var pageRect = new RectangleF(0, 0, size.Width, size.Height); var paragraphBounds = new RectangleF [page.Paragraphs.Count]; using (var ctxt = UIGraphics.GetCurrentContext()) { // fill background ctxt.SetFillColor(UIColor.FromHSBA(0.11f, 0.2f, 1, 1).CGColor); ctxt.FillRect(pageRect); pageRect = pageRect.Inset(20, 20); int i = 0; foreach (var p in page.Paragraphs) { var bounds = new RectangleF(pageRect.X, pageRect.Y, 0, 0); if (UnstyledDrawing) { var text = new NSString(page.StringForParagraph(p)); var font = UIFont.FromName("HoeflerText-Regular", 24); // draw text with the old legacy path, setting the font color to black. ctxt.SetFillColor(UIColor.Black.CGColor); bounds.Size = text.DrawString(pageRect, font); } else { // TODO: draw attributed text with new string drawing var text = page.AttributedStringForParagraph(p); var textContext = new NSStringDrawingContext(); text.DrawString(pageRect, NSStringDrawingOptions.UsesLineFragmentOrigin, textContext); bounds = textContext.TotalBounds; bounds.Offset(pageRect.X, pageRect.Y); } paragraphBounds [i++] = bounds; pageRect.Y += bounds.Height; } return(paragraphBounds); } }
public static UIImage ConvertFontIconToUIImage(string fontFamily, double fontSize, UIColor iconcolor, string glyph) { UIImage image = null; var cleansedname = CleanseFontName(fontFamily); var font = UIFont.FromName(cleansedname ?? string.Empty, (float)fontSize) ?? UIFont.SystemFontOfSize((float)fontSize); var attString = new NSAttributedString(glyph, font: font, foregroundColor: iconcolor); var imagesize = ((NSString)glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _)); UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f); var ctx = new NSStringDrawingContext(); var boundingRect = attString.GetBoundingRect(imagesize, 0, ctx); attString.DrawString(new DrawRect((float)(imagesize.Width / 2 - boundingRect.Size.Width / 2), (float)(imagesize.Height / 2 - boundingRect.Size.Height / 2), (float)imagesize.Width, (float)imagesize.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
public CommentItemView(float height, string nameText, string dateText, string commentText) : base() { Frame = new RectangleF(0, height, 670, 65); Image = new UIImageView(new RectangleF(32, 10, 50, 50)); Image.Layer.CornerRadius = 25f; Image.Layer.MasksToBounds = true; Add(Image); Name = Constants.makeLabel(new RectangleF(106, 10, 150, 17), UIColor.Black, UITextAlignment.Left, Font.Regular, 14); Name.Text = nameText; Name.AdjustsFontSizeToFitWidth = true; Add(Name); Date = Constants.makeLabel(new RectangleF(226, 10, 150, 17), UIColor.Gray, UITextAlignment.Left, Font.Regular, 10); Date.Text = dateText; Date.AdjustsFontSizeToFitWidth = true; Add(Date); Comment = Constants.makeLabel(new RectangleF(106, 30, 520, 22), UIColor.Gray, UITextAlignment.Left, Font.Regular, 14); Comment.Text = commentText; Comment.Lines = 0; Comment.LineBreakMode = UILineBreakMode.WordWrap; Add(Comment); /*resize height according to text*/ var nsText = new NSMutableAttributedString(Comment.Text); nsText.AddAttribute(UIStringAttributeKey.Font, Comment.Font, new NSRange(0, nsText.Length)); var ctxt = new NSStringDrawingContext(); SizeF expectedSize = nsText.GetBoundingRect(new SizeF(520, float.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, ctxt).Size; int expectedHeight = (int)Math.Round(expectedSize.Height); Comment.Frame = new RectangleF(106, 30, 520, expectedHeight); Frame = new RectangleF(0, height, 670, 50 + expectedHeight); }
/* * Render the page here: we assume we are already in a normalized coordinate system which maps * our standard aspect ratio (3:4) to (1:1) * The reason why we do this is to reuse the same drawing code for both the preview and the * full screen; for full screen rendering, we map the whole view, whereas the preview maps * the whole preview image to a quarter of the page. * */ public RectangleF [] RenderPage (Page page, SizeF size, bool unstyledDrawing) { var pageRect = new RectangleF (0, 0, size.Width, size.Height); var paragraphBounds = new RectangleF [page.Paragraphs.Count]; using (var ctxt = UIGraphics.GetCurrentContext ()) { // fill background ctxt.SetFillColor (UIColor.FromHSBA (0.11f, 0.2f, 1, 1).CGColor); ctxt.FillRect (pageRect); pageRect = pageRect.Inset (20, 20); int i = 0; foreach (var p in page.Paragraphs) { var bounds = new RectangleF (pageRect.X, pageRect.Y, 0, 0); if (UnstyledDrawing) { var text = new NSString (page.StringForParagraph (p)); var font = UIFont.FromName ("HoeflerText-Regular", 24); // draw text with the old legacy path, setting the font color to black. ctxt.SetFillColor (UIColor.Black.CGColor); bounds.Size = text.DrawString (pageRect, font); } else { // TODO: draw attributed text with new string drawing var text = page.AttributedStringForParagraph (p); var textContext = new NSStringDrawingContext (); text.DrawString (pageRect, NSStringDrawingOptions.UsesLineFragmentOrigin, textContext); bounds = textContext.TotalBounds; bounds.Offset (pageRect.X, pageRect.Y); } paragraphBounds [i++] = bounds; pageRect.Y += bounds.Height; } return paragraphBounds; } }