private UIImage GetIconForText(string text, nuint bucketIndex) { var nsText = new NSString(text); var icon = _iconCache.ObjectForKey(nsText); if (icon != null) { return((UIImage)icon); } var font = UIFont.BoldSystemFontOfSize(14); var paragraphStyle = NSParagraphStyle.Default; var dict = NSDictionary.FromObjectsAndKeys( objects: new NSObject[] { font, paragraphStyle, this._options.RendererTextColor.ToUIColor() }, keys: new NSObject[] { UIStringAttributeKey.Font, UIStringAttributeKey.ParagraphStyle, UIStringAttributeKey.ForegroundColor } ); var attributes = new UIStringAttributes(dict); var textSize = nsText.GetSizeUsingAttributes(attributes); var rectDimension = Math.Max(20, Math.Max(textSize.Width, textSize.Height)) + 3 * bucketIndex + 6; var rect = new CGRect(0.0f, 0.0f, rectDimension, rectDimension); UIGraphics.BeginImageContext(rect.Size); UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0); // Background circle var ctx = UIGraphics.GetCurrentContext(); ctx.SaveState(); bucketIndex = (nuint)Math.Min((int)bucketIndex, this._options.BucketColors.Length - 1); var backColor = this._options.BucketColors[bucketIndex]; ctx.SetFillColor(backColor.ToCGColor()); ctx.FillEllipseInRect(rect); ctx.RestoreState(); // Draw the text UIColor.White.SetColor(); var textRect = RectangleFExtensions.Inset(rect, (rect.Size.Width - textSize.Width) / 2, (rect.Size.Height - textSize.Height) / 2); nsText.DrawString(RectangleFExtensions.Integral(textRect), attributes); var newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); this._iconCache.SetObjectforKey(newImage, nsText); return(newImage); }
private UIImage GetIconForText(string text, UIImage baseImage) { var nsText = new NSString(text); var icon = iconCache.ObjectForKey(nsText); if (icon != null) { return((UIImage)icon); } var font = UIFont.BoldSystemFontOfSize(12); var size = baseImage.Size; UIGraphics.BeginImageContextWithOptions(size, false, 0.0f); baseImage.Draw(new CGRect(0, 0, size.Width, size.Height)); var rect = new CGRect(0, 0, baseImage.Size.Width, baseImage.Size.Height); var paragraphStyle = NSParagraphStyle.Default; var attributes = new UIStringAttributes(NSDictionary.FromObjectsAndKeys( objects: new NSObject[] { font, paragraphStyle, options.RendererTextColor.ToUIColor() }, keys: new NSObject[] { UIStringAttributeKey.Font, UIStringAttributeKey.ParagraphStyle, UIStringAttributeKey.ForegroundColor } )); var textSize = nsText.GetSizeUsingAttributes(attributes); var textRect = RectangleFExtensions.Inset(rect, (rect.Size.Width - textSize.Width) / 3, (rect.Size.Height - textSize.Height) / 1); nsText.DrawString(RectangleFExtensions.Integral(textRect), attributes); var newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); iconCache.SetObjectforKey(newImage, nsText); return(newImage); }
public void Show(ToastType type) { var image = this.theSettings().images[type]; var font = UIFont.SystemFontOfSize(this.theSettings().fontSize); var str = new NSAttributedString(this.text, font); var textSize = str.GetBoundingRect(new CGSize(260, 50), NSStringDrawingOptions.UsesLineFragmentOrigin, null).Size; textSize.Width = textSize.Width + 5; var label = new UILabel(new CGRect(0, 0, textSize.Width + this.LeftAndRightPadding, textSize.Height + this.TopAndBottomPadding)) { BackgroundColor = UIColor.Clear, TextColor = UIColor.White, TextAlignment = UITextAlignment.Left, Text = this.text, Lines = 0, Font = UIFont.SystemFontOfSize(this.theSettings().fontSize) }; if (this.theSettings().useShadow) { label.ShadowColor = UIColor.DarkGray; label.ShadowOffset = new CGSize(1, 1); } var button = new UIButton(UIButtonType.Custom); if (image != null) { button.Frame = this.ToastFrameForImageSize(image.Size, textSize); /*switch (this.theSettings().imageLocation) * { * case ToastImageLocation.Left:*/ label.TextAlignment = UITextAlignment.Left; label.Center = new CGPoint(image.Size.Width + (this.LeftAndRightPadding * 2) + (((button.Frame.Size.Width - image.Size.Width) - (this.LeftAndRightPadding * 2)) / 2), button.Frame.Size.Height / 2); /* break; * * case ToastImageLocation.Top: * label.TextAlignment = UITextAlignment.Center; * label.Center = new CGPoint(button.Frame.Size.Width / 2, (image.Size.Height + (this.TopAndBottomPadding * 2)) + (((button.Frame.Size.Height - image.Size.Height) - (this.TopAndBottomPadding * 2)) / 2)); * break; * }*/ } else { button.Frame = new CGRect(0, 0, textSize.Width + (this.LeftAndRightPadding * 2), textSize.Height + (this.TopAndBottomPadding * 2)); label.Center = new CGPoint(button.Frame.Size.Width / 2, button.Frame.Size.Height / 2); } var rect8 = label.Frame; rect8.X = (nfloat)Math.Ceiling((double)rect8.X); rect8.Y = (nfloat)Math.Ceiling((double)rect8.Y); label.Frame = rect8; button.AddSubview(label); if (image != null) { var view = new UIImageView(image); view.Frame = this.FrameForImage(type, button.Frame); button.AddSubview(view); } button.BackgroundColor = UIColor.FromRGBA(this.theSettings().bgRed, this.theSettings().bgGreen, this.theSettings().bgBlue, this.theSettings().bgAlpha); button.Layer.CornerRadius = this.theSettings().cornerRadius; var window = UIApplication.SharedApplication.Windows[0]; var empty = CGPoint.Empty; var orientation = UIApplication.SharedApplication.StatusBarOrientation; switch (this.theSettings().gravity) { case ToastGravity.Top: empty = new CGPoint(window.Frame.Size.Width / 2, 0x2d); break; case ToastGravity.Bottom: empty = new CGPoint(window.Frame.Size.Width / 2, window.Frame.Size.Height - 0x2d); break; case ToastGravity.Center: empty = new CGPoint(window.Frame.Size.Width / 2, window.Frame.Size.Height / 2); break; } empty = new CGPoint(empty.X + this.theSettings().offsetLeft, empty.Y + this.theSettings().offsetTop); button.Center = empty; button.Frame = RectangleFExtensions.Integral(button.Frame); var timer = NSTimer.CreateTimer(TimeSpan.FromSeconds(this.theSettings().duration / 1000), t => this.HideToast()); NSRunLoop.Main.AddTimer(timer, 0); button.Tag = (nint)CURRENT_TOAST_TAG; var view2 = window.ViewWithTag((nint)CURRENT_TOAST_TAG); if (view2 != null) { view2.RemoveFromSuperview(); } button.Alpha = 0; window.AddSubview(button); UIView.BeginAnimations(null, IntPtr.Zero); button.Alpha = 1; UIView.CommitAnimations(); this.view = button; button.AddTarget(new EventHandler(this.HideToastEventHandler), UIControlEvent.TouchDown); ToastSetting.SharedSettings = null; }