public static UIImage ImageFromFont(string text, UIColor iconColor, CGSize iconSize, string fontName) { UIGraphics.BeginImageContextWithOptions(iconSize, false, 0); var textRect = new CGRect(CGPoint.Empty, iconSize); var path = UIBezierPath.FromRect(textRect); UIColor.Clear.SetFill(); path.Fill(); var font = UIFont.FromName(fontName, iconSize.Width); using (var label = new UILabel() { Text = text, Font = font }) { GetFontSize(label, iconSize, 500, 5); font = label.Font; } iconColor.SetFill(); using (var nativeString = new NSString(text)) { nativeString.DrawString(textRect, new UIStringAttributes { Font = font, ForegroundColor = iconColor, BackgroundColor = UIColor.Clear, ParagraphStyle = new NSMutableParagraphStyle { Alignment = UITextAlignment.Center } }); } var image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); image = image.ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal); return image; }
private static UIImage CreateBackgroundImage(UIColor color) { UIGraphics.BeginImageContext(new CoreGraphics.CGSize(1, 1f)); color.SetFill(); UIGraphics.RectFill(new CoreGraphics.CGRect(0, 0, 1, 1)); var img = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return img; }
static UIImage RenderSolidImage(UIColor color, CGSize size) { UIGraphics.BeginImageContextWithOptions (size, false, 0); CGContext context = UIGraphics.GetCurrentContext (); color.SetFill (); context.FillRect (new CGRect (CGPoint.Empty, size)); UIImage result = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return result; }
public static UIImage ImageFromFont(UIFont font, char character, UIColor fillColor) { var s = new NSString("" + character); var stringSize = s.StringSize(font); var maxSize = (nfloat)Math.Max(stringSize.Height, stringSize.Width); var size = new CGSize(maxSize, maxSize); UIGraphics.BeginImageContextWithOptions(size, false, 0f); fillColor.SetFill(); var drawPoint = new CGPoint((size.Width / 2f) - (stringSize.Width / 2f), (size.Height / 2f) - (stringSize.Height / 2f)); s.DrawString(drawPoint, font); var img = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return img; }
public static UIImage Tint(this UIImage img, UIColor tint, CGBlendMode blendMode) { UIGraphics.BeginImageContextWithOptions(img.Size, false, 0f); tint.SetFill(); var bounds = new CGRect(0, 0, img.Size.Width, img.Size.Height); UIGraphics.RectFill(bounds); img.Draw(bounds, blendMode, 1f); if (blendMode != CGBlendMode.DestinationIn) img.Draw(bounds, CGBlendMode.DestinationIn, 1f); var tintedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return tintedImage; }
// private methods UIImage CircleImageWithColor(UIColor color, nfloat height) { UIGraphics.BeginImageContextWithOptions(new CGSize(height, height), false, 0); CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); UIBezierPath fillPath = UIBezierPath.FromOval(new CGRect(0, 0, height, height)); color.SetFill(); fillPath.Fill(); UIImage dotImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return dotImage; }