Skip to content

Commit

Permalink
Toast enhancements
Browse files Browse the repository at this point in the history
- Allow position of toast (and other messages) to be center, top, or
bottom
- Allow change of HUD text color
  • Loading branch information
danmiser committed Feb 6, 2014
1 parent 2e9db57 commit f39366c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions BTProgressHUD/BTProgressHUD.cs
Expand Up @@ -20,9 +20,9 @@ public static void ShowContinuousProgress (string status = null, ProgressHUD.Mas
ProgressHUD.Shared.ShowContinuousProgress (status, maskType);
}

public static void ShowToast (string status, bool showToastCentered = true, double timeoutMs = 1000)
public static void ShowToast (string status, ProgressHUD.ToastPosition toastPosition = ProgressHUD.ToastPosition.Center, double timeoutMs = 1000)
{
ProgressHUD.Shared.ShowToast (status, showToastCentered, timeoutMs);
ProgressHUD.Shared.ShowToast (status, toastPosition, timeoutMs);
}

public static void SetStatus (string status)
Expand Down
37 changes: 27 additions & 10 deletions BTProgressHUD/ProgressHUD.cs
Expand Up @@ -67,9 +67,17 @@ public enum MaskType
Gradient
}

public enum ToastPosition
{
Bottom = 1,
Center,
Top
}

public UIColor HudBackgroundColour = UIColor.FromWhiteAlpha (0.0f, 0.8f);
public UIColor HudForegroundColor = UIColor.White;
public UIColor HudStatusShadowColor = UIColor.Black;
public UIColor HudToastBackgroundColor = UIColor.Clear;
public UIFont HudFont = UIFont.BoldSystemFontOfSize (16f);
public Ring Ring = new Ring ();
static NSObject obj = new NSObject ();
Expand All @@ -93,12 +101,12 @@ public void Show (string status = null, float progress = -1, MaskType maskType =

public void ShowContinuousProgress (string status = null, MaskType maskType = MaskType.None, double timeoutMs = 1000)
{
obj.InvokeOnMainThread (() => ShowProgressWorker (0, status, maskType, false, true, null, null, timeoutMs, true));
obj.InvokeOnMainThread (() => ShowProgressWorker (0, status, maskType, false, ToastPosition.Center, null, null, timeoutMs, true));
}

public void ShowToast (string status, bool showToastCentered = true, double timeoutMs = 1000)
public void ShowToast (string status, ToastPosition toastPosition = ToastPosition.Center, double timeoutMs = 1000)
{
obj.InvokeOnMainThread (() => ShowProgressWorker (status: status, textOnly: true, showToastCentered: showToastCentered, timeoutMs: timeoutMs));
obj.InvokeOnMainThread (() => ShowProgressWorker (status: status, textOnly: true, toastPosition: toastPosition, timeoutMs: timeoutMs));
}

public void SetStatus (string status)
Expand Down Expand Up @@ -220,7 +228,7 @@ public override void Draw (RectangleF rect)
}

void ShowProgressWorker (float progress = -1, string status = null, MaskType maskType = MaskType.None, bool textOnly = false,
bool showToastCentered = true, string cancelCaption = null, Action cancelCallback = null,
ToastPosition toastPosition = ToastPosition.Center, string cancelCaption = null, Action cancelCallback = null,
double timeoutMs = 1000, bool showContinuousProgress = false)
{
if (OverlayView.Superview == null)
Expand Down Expand Up @@ -302,7 +310,7 @@ public override void Draw (RectangleF rect)
}

OverlayView.Hidden = false;
this.showToastCentered = showToastCentered;
this.toastPosition = toastPosition;
PositionHUD (null);


Expand Down Expand Up @@ -552,7 +560,7 @@ UILabel StringLabel
if (_stringLabel == null)
{
_stringLabel = new UILabel ();
_stringLabel.BackgroundColor = UIColor.Clear;
_stringLabel.BackgroundColor = HudToastBackgroundColor;
_stringLabel.AdjustsFontSizeToFitWidth = true;
_stringLabel.TextAlignment = UITextAlignment.Center;
_stringLabel.BaselineAdjustment = UIBaselineAdjustment.AlignCenters;
Expand Down Expand Up @@ -776,7 +784,7 @@ void MoveToPoint (PointF newCenter, float angle)
HudView.Center = newCenter;
}

bool showToastCentered = true;
ToastPosition toastPosition = ToastPosition.Center;

void PositionHUD (NSNotification notification)
{
Expand Down Expand Up @@ -823,11 +831,20 @@ void PositionHUD (NSNotification notification)
float posY = (float)Math.Floor (activeHeight * 0.45);
float posX = orientationFrame.Size.Width / 2;

if (!showToastCentered)
switch (toastPosition)
{
posY = activeHeight - 40;
case ToastPosition.Bottom:
posY = activeHeight - 40;
break;
case ToastPosition.Center:
// Already set above
break;
case ToastPosition.Top:
posY = 40;
default:
break;
}

PointF newCenter;
float rotateAngle;

Expand Down
5 changes: 3 additions & 2 deletions BTProgressHUDDemo/MainView.cs
Expand Up @@ -106,8 +106,9 @@ public override void LoadView ()
});

MakeButton ("Toast", () => {
ProgressHUD.Shared.ShowToast ("Hello from the toast", showToastCentered: false);
ProgressHUD.Shared.HudForegroundColor = UIColor.White;
ProgressHUD.Shared.HudToastBackgroundColor = UIColor.DarkGray;
ProgressHUD.Shared.ShowToast ("Hello from the toast\r\nLine 2", toastPosition: ProgressHUD.ToastPosition.Top, timeoutMs: 3000);
});

MakeButton ("Progress", () => {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -71,10 +71,10 @@ public enum MaskType
```

###ShowToast
The toast can be centered or at the bottom of the screen, like Android. This is controlled by the second parameter.
The toast can be centered or at the top or bottom of the screen. This is controlled by the second parameter.

```csharp
BTProgressHUD.ShowToast("foo", showToastCentered: false);
BTProgressHUD.ShowToast("foo", toastPosition: ProgressHUD.ToastPosition.Center);
```

###ShowSuccess/Error/ShowImage
Expand Down

0 comments on commit f39366c

Please sign in to comment.