// // JKToast.m // JKToast // // Created by Jakey on 14-10-27. // Copyright (c) 2014年 www.skyfox.org. All rights reserved. // #import "JKToast.h" #import @implementation JKToast - (id)initWithText:(NSString *)text{ if (self = [super init]) { text = [text copy]; UIFont *font = [UIFont systemFontOfSize:14]; // CGSize textSize = [text sizeWithFont:font // constrainedToSize:CGSizeMake(280, MAXFLOAT) // lineBreakMode:NSLineBreakByCharWrapping]; CGSize textSize = [text boundingRectWithSize:CGSizeMake(280, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size; CGRect textFrame = CGRectMake(0, 0, textSize.width + 40, textSize.height + 30); if(textLab){ textLab.frame = textFrame; }else{ textLab = [[UILabel alloc] initWithFrame:textFrame]; textLab.backgroundColor = [UIColor clearColor]; textLab.textColor = [UIColor darkTextColor]; textLab.textAlignment = NSTextAlignmentCenter; textLab.numberOfLines = 0; textLab.font = font; } textLab.text = text; if (contentView) { contentView.frame = textFrame; }else{ contentView = [[UIButton alloc] initWithFrame:textFrame]; contentView.alpha = 0.0f; contentView.layer.cornerRadius = 23.0f; contentView.backgroundColor = RDSLightGrayColor; contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth; [contentView addSubview:textLab]; [contentView addTarget:self action:@selector(toastTaped:) forControlEvents:UIControlEventTouchDown]; duration = DEFAULT_DISPLAY_DURATION; } // [[NSNotificationCenter defaultCenter] addObserver:self // selector:@selector(deviceOrientationDidChanged:) // name:UIDeviceOrientationDidChangeNotification // object:[UIDevice currentDevice]]; } return self; } - (void)deviceOrientationDidChanged:(NSNotification *)notify{ UIDevice* device = [notify valueForKey:@"object"]; //[self hideAnimation]; } -(void)toastTaped:(UIButton *)sender{ [self hideAnimation]; } - (void)setDuration:(CGFloat) duration{ duration = duration; } -(void)showAnimation{ [UIView animateWithDuration:0.3 animations:^{ self->contentView.alpha = 1.0f; } completion:^(BOOL finished) { }]; } -(void)hideAnimation{ [UIView animateWithDuration:0.3 animations:^{ self->contentView.alpha = 0.0f; } completion:^(BOOL finished) { }]; } - (void)showWithLocal:(iToastLocal)local{ // 取消延时任务 dispatch_async(dispatch_get_main_queue(), ^{ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideAnimation) object:nil]; }); UIWindow *window = [UIApplication sharedApplication].delegate.window; [window endEditing:YES]; CGFloat centerY; if(local == localCenter){ centerY = window.center.y; }else{ centerY = window.bounds.size.height -160; } CGPoint center = CGPointMake(window.center.x, centerY); contentView.center = center; [window addSubview:contentView]; [self showAnimation]; dispatch_async(dispatch_get_main_queue(), ^{ [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self->duration]; }); } - (void)showFromTopOffset:(CGFloat) top{ UIWindow *window = [UIApplication sharedApplication].delegate.window; contentView.center = CGPointMake(window.center.x, top + contentView.frame.size.height/2); [window addSubview:contentView]; [self showAnimation]; [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration]; } - (void)showFromBottomOffset:(CGFloat) bottom{ UIWindow *window = [UIApplication sharedApplication].delegate.window; contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom + contentView.frame.size.height/2)); [window addSubview:contentView]; [self showAnimation]; [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration]; } + (JKToast*)sharedView { static dispatch_once_t once; static JKToast *sharedView; dispatch_once(&once, ^{ sharedView = [[self alloc] init]; }); return sharedView; } + (void)showTopWithText:(NSString *)text{ [JKToast showWithText:text local:localTop duration:3]; } + (void)showBottomWithText:(NSString *)text{ [JKToast showWithText:text local:localBottom duration:3]; } + (void)showCenterWithText:(NSString *)text{ [JKToast showWithText:text local:localCenter duration:3]; } + (void)showWithText:(NSString *)text local:(iToastLocal)local duration:(CGFloat)duration{ JKToast *toast = [[self sharedView] initWithText:text]; [toast setDuration:duration]; [toast showWithLocal:local]; } + (void)showWithText:(NSString *)text topOffset:(CGFloat)topOffset{ [JKToast showWithText:text topOffset:topOffset duration:DEFAULT_DISPLAY_DURATION]; } + (void)showWithText:(NSString *)text topOffset:(CGFloat)topOffset duration:(CGFloat)duration{ JKToast *toast = [[JKToast alloc] initWithText:text]; [toast setDuration:duration]; [toast showFromTopOffset:topOffset]; } + (void)showWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset{ [JKToast showWithText:text bottomOffset:bottomOffset duration:DEFAULT_DISPLAY_DURATION]; } + (void)showWithText:(NSString *)text bottomOffset:(CGFloat)bottomOffset duration:(CGFloat)duration{ JKToast *toast = [[JKToast alloc] initWithText:text] ; [toast setDuration:duration]; [toast showFromBottomOffset:bottomOffset]; } @end