Android Toast in iPhone?

后端 未结 12 826
轮回少年
轮回少年 2020-12-05 20:38

When I write Android apps, I love the Toast feature. Is there a way to get this kind of set and forget popup message in iPhone development using MonoTouch (C# .NET)?

相关标签:
12条回答
  • 2020-12-05 20:51

    You could try my open source library TSMessages: https://github.com/toursprung/TSMessages

    It's really easy to use and looks beautiful on iOS 5/6 and on iOS 7 as well.

    0 讨论(0)
  • 2020-12-05 20:54

    MonoTouch Toast Version here. Inspired by Android.

    To call it,

            ToastView t = new ToastView ("Email Sent", 1000);
            t.Show ();
    

    Enum File:

    public enum ToastGravity
    {
        Top = 0,
        Bottom = 1,
        Center = 2
    }
    

    ToastSettings File:

    using System;
    using System.Drawing;
    using MonoTouch.UIKit;
    namespace General
    {
    
        public class ToastSettings
        {
            public ToastSettings ()
            {
                this.Duration = 500;
                this.Gravity = ToastGravity.Center;
            }
    
            public int Duration
            {
                get;
                set;
            }
    
            public double DurationSeconds
            {
                get { return (double) Duration/1000 ;}
    
            }
    
            public ToastGravity Gravity
            {
                get;
                set;
            }
    
            public PointF Position
            {
                get;
                set;
            }
    
    
        }
    }
    

    Main Toast Class:

    using System;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    using System.Drawing;
    using MonoTouch.ObjCRuntime;
    
    namespace General
    {
        public class ToastView : NSObject
        {
    
            ToastSettings theSettings = new ToastSettings ();
    
            private string text = null;
            UIView view;
            public ToastView (string Text, int durationMilliseonds)
            {
                text = Text;
                theSettings.Duration = durationMilliseonds;
            }
    
            int offsetLeft = 0;
            int offsetTop = 0;
            public ToastView SetGravity (ToastGravity gravity, int OffSetLeft, int OffSetTop)
            {
                theSettings.Gravity = gravity;
                offsetLeft = OffSetLeft;
                offsetTop = OffSetTop;
                return this;
            }
    
            public ToastView SetPosition (PointF Position)
            {
                theSettings.Position = Position;
                return this;
            }
    
            public void Show ()
            {
                UIButton v = UIButton.FromType (UIButtonType.Custom);
                view = v;
    
                UIFont font = UIFont.SystemFontOfSize (16);
                SizeF textSize = view.StringSize (text, font, new SizeF (280, 60));
    
                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);
    
    
                v.Frame = new RectangleF (0, 0, textSize.Width + 10, textSize.Height + 10);
                label.Center = new PointF (v.Frame.Size.Width / 2, v.Frame.Height / 2);
                v.AddSubview (label);
    
                v.BackgroundColor = UIColor.FromRGBA (0, 0, 0, 0.7f);
                v.Layer.CornerRadius = 5;
    
                UIWindow window = UIApplication.SharedApplication.Windows[0];
    
                PointF point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
    
                if (theSettings.Gravity == ToastGravity.Top)
                {
                    point = new PointF (window.Frame.Size.Width / 2, 45);
                }
                else if (theSettings.Gravity == ToastGravity.Bottom)
                {
                    point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height - 45);
                }
                else if (theSettings.Gravity == ToastGravity.Center)
                {
                    point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
                }
                else
                {
                    point = theSettings.Position;
                }
    
                point = new PointF (point.X + offsetLeft, point.Y + offsetTop);
                v.Center = point;
                window.AddSubview (v);
                v.AllTouchEvents += delegate { HideToast (null); };
    
                NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);
    
            }
    
    
            void HideToast ()
            {
                UIView.BeginAnimations ("");
                view.Alpha = 0;
                UIView.CommitAnimations ();
            }
    
            void RemoveToast ()
            {
                view.RemoveFromSuperview ();
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-05 20:54

    I modified John's answer as follows:

    Toast.h

    @interface Toast : NSObject
    
    + (void)toast:(NSString *)message
                 :(UIView *) view
                 :(int)delay;
    
    @end
    

    Toast.m

    #import "Toast.h"
    
    @interface Toast ()
    
    @end
    
    @implementation Toast
    
    + (void)toast:(NSString *)message
                 :(UIView *) view
                 :(int)delay
    {
        CGRect initialFrame = CGRectMake(10, view.frame.size.height/2, 300, 40);
        UILabel *flashLabel=[[UILabel alloc] initWithFrame:initialFrame];
        flashLabel.font=[UIFont fontWithName:@"Optima-Italic" size:19.0];
        flashLabel.backgroundColor=[UIColor whiteColor];
        flashLabel.layer.cornerRadius=9.0f;
        flashLabel.clipsToBounds = YES;
        flashLabel.numberOfLines=3;
        flashLabel.textAlignment=NSTextAlignmentCenter;
        CGSize maxSize = CGSizeMake(flashLabel.frame.size.width, MAXFLOAT);
        CGRect labelRect = [message boundingRectWithSize:maxSize
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:@{NSFontAttributeName:flashLabel.font}
                                                 context:nil];
    
        //adjust the label the the new height.
        CGRect newFrame = flashLabel.frame;
        newFrame.size.height = labelRect.size.height * 2;
        flashLabel.frame = newFrame;
        flashLabel.text=message;
        [view addSubview:flashLabel];
        flashLabel.alpha=1.0;
        view.userInteractionEnabled=FALSE;
    
        [UIView animateWithDuration:delay animations:^
        {
            flashLabel.alpha=0.0f;
        }
    
        completion:^(BOOL finished)
        {
            view.userInteractionEnabled=TRUE;
            [flashLabel removeFromSuperview];
        }];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-05 20:55

    You might be after Local Notifications, pretty sure they allow you to set a time, I think in epoch time to be fired off. Don't think there is a way to hide them though. I might be misunderstanding your question though cause I'm unfamiliar with Toast.

    0 讨论(0)
  • 2020-12-05 20:59

    You can use this link for objective-c code for Toast

    http://code.google.com/p/toast-notifications-ios/source/browse/trunk/

    While this link for its usage

    http://code.google.com/p/toast-notifications-ios/wiki/HowToUse

    which could be like any one of the below samples

    [[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")] show];
    
    [[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")] 
                              setGravity:iToastGravityBottom] show];
    
    [[[[iToast makeText:NSLocalizedString(@"Something to display a very long time", @"")] 
                      etGravity:iToastGravityBottom] setDuration:iToastDurationLong] show];
    
    0 讨论(0)
  • 2020-12-05 21:01

    Are you looking for something like UIAlertView?

    0 讨论(0)
提交回复
热议问题