Skip to content

Instantly share code, notes, and snippets.

@cjgaliana
Forked from davidroth/ToastView.cs
Last active August 29, 2015 14:05
Show Gist options
  • Save cjgaliana/bb79622a1928b7c3d8d9 to your computer and use it in GitHub Desktop.
Save cjgaliana/bb79622a1928b7c3d8d9 to your computer and use it in GitHub Desktop.
// Licence: MIT X11
// Note: This is an Objective-C to C# translation by using code from: http://code.google.com/p/toast-notifications-ios/
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System;
using System.Drawing;
namespace Tabasco.iOS.Dialog
{
/// <summary>
/// Usage:
/// ToastView view = new ToastView("Event added to your schedule", 1000);
/// view.SetGravity(ToastGravity.Bottom);
/// view.Show();
/// </summary>
public class ToastView
{
private const int MAX_TOAST_LINES = 5; //Support for multilines toasts. Max 5 lines
private const int TOAST_WITHD = 280; //Total withd of the toast rectangle
private const int TOAST_LINE_HEIGHT = 30; //Line Height
private const double FADE_OUT_DURATION = 1.0; //Fade out animation duration
private ToastSettings toastSettings = new ToastSettings();
private string text = null;
private UIView view;
private int offsetLeft = 0;
private int offsetTop = 0;
/// <summary>
/// Initializes a new instance of the <see cref="ToastView"/> class.
/// </summary>
/// <param name="content">The toast text</param>
/// <param name="duration">The toast duration.</param>
public ToastView(string content, TimeSpan duration)
{
text = content;
toastSettings.Duration = duration;
}
/// <summary>
/// Sets the Toast notification gravity
/// </summary>
/// <param name="gravity">The position in the screen</param>
/// <param name="OffSetLeft">Left offset</param>
/// <param name="OffSetTop">Top offset</param>
/// <returns>The ToasView. Use of Fluent API</returns>
public ToastView SetGravity(ToastGravity gravity, int OffSetLeft = 0, int OffSetTop = 0)
{
toastSettings.Gravity = gravity;
offsetLeft = OffSetLeft;
offsetTop = OffSetTop;
return this;
}
/// <summary>
/// Sets the toast notification position in the screen ()
/// </summary>
/// <param name="Position">The toast position in the screen</param>
/// <returns>The ToastView. Use of fluent API</returns>
public ToastView SetPosition(PointF Position)
{
toastSettings.Position = new TPoint(Position.X, Position.Y);
return this;
}
/// <summary>
/// Shows the Toast Notification
/// </summary>
public void Show()
{
UIButton toast = UIButton.FromType(UIButtonType.Custom);
view = toast;
UIFont font = UIFont.SystemFontOfSize(12);
//Multiline support
int lineCount = Math.Min(MAX_TOAST_LINES, text.Split('\n').Length + 1);
SizeF textSize = view.StringSize(text, font, new SizeF(TOAST_WITHD, TOAST_LINE_HEIGHT * lineCount));
UILabel label = new UILabel(new RectangleF(0, 0, textSize.Width + 5, textSize.Height + 5));
label.BackgroundColor = UIColor.Clear;
label.TextColor = UIColor.White;
label.Font = font;
label.Text = text;
label.Lines = 0;
label.ShadowColor = UIColor.DarkGray;
label.ShadowOffset = new SizeF(1, 1);
toast.Frame = new RectangleF(0, 0, textSize.Width + 10, textSize.Height + 10);
label.Center = new PointF(toast.Frame.Size.Width / 2, toast.Frame.Height / 2);
toast.AddSubview(label);
toast.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0.7f);
toast.Layer.CornerRadius = 5;
UIWindow window = UIApplication.SharedApplication.Windows[0];
PointF point;
if (toastSettings.Gravity == ToastGravity.Top)
{
point = new PointF(window.Frame.Size.Width / 2, (textSize.Height / 2) + 35);
}
else if (toastSettings.Gravity == ToastGravity.Bottom)
{
point = new PointF(window.Frame.Size.Width / 2, window.Frame.Size.Height - (textSize.Height / 2) - 35);
}
else if (toastSettings.Gravity == ToastGravity.Center)
{
point = new PointF(window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
}
else
{
point = new PointF(toastSettings.Position.X, toastSettings.Position.Y);
}
point = new PointF(point.X + offsetLeft, point.Y + offsetTop);
toast.Center = point;
window.AddSubview(toast);
// Dismiss the toast if the user touches the toast rectablge
toast.AllTouchEvents += delegate { HideToast(); };
//Auto hide after toast duration expires
NSTimer.CreateScheduledTimer(toastSettings.Duration.TotalSeconds, () => this.HideToast());
}
/// <summary>
/// Hides the toast using a fade out animation
/// </summary>
private void HideToast()
{
//Create the fade out animation.
UIView.Animate(FADE_OUT_DURATION, 0, UIViewAnimationOptions.TransitionNone,
() =>
{
//End status of the animation
this.view.Alpha = 0;
},
() =>
{
//What happens when the animation ends
this.RemoveToast();
}
);
}
/// <summary>
/// Removes the toast from the view
/// </summary>
private void RemoveToast()
{
view.RemoveFromSuperview();
}
}
/// <summary>
/// Toast Notification Settigns
/// </summary>
public class ToastSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="ToastSettings"/> class.
/// </summary>
public ToastSettings()
{
this.Duration = TimeSpan.FromSeconds(1);
this.Gravity = ToastGravity.Center;
}
/// <summary>
/// Initializes a new instance of the <see cref="ToastSettings"/> class.
/// </summary>
/// <param name="duration">The toast duration.</param>
/// <param name="gravity">The toast gravity.</param>
public ToastSettings(TimeSpan duration, ToastGravity gravity = ToastGravity.Center)
{
this.Duration = duration;
this.Gravity = gravity;
}
/// <summary>
/// Gets or sets the toast duration
/// </summary>
/// <value>
/// The duration.
/// </value>
public TimeSpan Duration
{
get;
set;
}
/// <summary>
/// Gets or sets the toast gravity (position in the view).
/// </summary>
/// <value>
/// The gravity.
/// </value>
public ToastGravity Gravity
{
get;
set;
}
/// <summary>
/// Gets or sets the toast position.
/// </summary>
/// <value>
/// The position.
/// </value>
public TPoint Position
{
get;
set;
}
}
/// <summary>
/// Defines a Point.
/// FPoint is not supported in Portable Class Libraries
/// </summary>
public class TPoint
{
/// <summary>
/// Initializes a new instance of the <see cref="TPoint"/> class.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
public TPoint(float x, float y)
{
this.X = x;
this.Y = y;
}
/// <summary>
/// Gets or sets the x.
/// </summary>
/// <value>
/// The x.
/// </value>
public float X { get; set; }
/// <summary>
/// Gets or sets the y.
/// </summary>
/// <value>
/// The y.
/// </value>
public float Y { get; set; }
}
/// <summary>
/// The Toast Gravity enum
/// </summary>
public enum ToastGravity
{
Top = 0,
Bottom = 1,
Center = 2
}
}
@cjgaliana
Copy link
Author

Adds fade out animation to the toasts + multi line support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment