Android Toast in iPhone?

2019-01-14 12:57发布

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条回答
Root(大扎)
2楼-- · 2019-01-14 13:46

Are you looking for something like UIAlertView?

查看更多
萌系小妹纸
3楼-- · 2019-01-14 13:50

I have added a little modification to the toast class that handles rotation of the display.

        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;
        //handle screen rotation
        float orientation=0;

        switch(UIApplication.SharedApplication.StatusBarOrientation)
        {
        case UIInterfaceOrientation.LandscapeLeft:
            orientation=-90;
            break;
        case UIInterfaceOrientation.LandscapeRight:
            orientation=90;
            break;
        case UIInterfaceOrientation.PortraitUpsideDown:
            orientation=180;
            break;
        }
        v.Transform=CGAffineTransform.MakeRotation ((float)(orientation / 180f * Math.Pi));
        window.AddSubview (v);
        v.AllTouchEvents += delegate { HideToast (); };

        NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);

    }
查看更多
孤傲高冷的网名
4楼-- · 2019-01-14 13:53

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.

查看更多
男人必须洒脱
5楼-- · 2019-01-14 13:56

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
查看更多
叼着烟拽天下
6楼-- · 2019-01-14 13:57

Just You can use the following code with uilabel and uianimation to get toast like in android. It does two works one is toast task and it increases the height of the label according to the text length with wordwrap IOS 7 later link here

CGRect initialFrame = CGRectMake(20, self.view.frame.size.height/2,300, 40);


NSString *message=@"Toast in Iphone as in Android";
UILabel *flashLabel=[[UILabel alloc] initWithFrame:initialFrame];
flashLabel.font=[UIFont fontWithName:@"Optima-Italic" size:12.0];
flashLabel.backgroundColor=[UIColor whiteColor];
flashLabel.layer.cornerRadius=3.0f;
flashLabel.numberOfLines=0;
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;
flashLabel.frame = newFrame;
flashLabel.text=message;
[self.view addSubview:flashLabel];

flashLabel.alpha=1.0;
self.view.userInteractionEnabled=FALSE;

[UIView animateWithDuration:13.0 animations:^
{
    flashLabel.alpha=0.0f;
}
completion:^(BOOL finished)
{
    self.view.userInteractionEnabled=TRUE;

    [flashLabel removeFromSuperview];
}];
查看更多
SAY GOODBYE
7楼-- · 2019-01-14 14:00

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.

查看更多
登录 后发表回答