iOS实现吐司效果
+ (void)tostWithMessage:(NSString *)message {
CGSize msgSize = [message boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-100, [UIScreen mainScreen].bounds.size.height/4)
options:
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
context:nil].size;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
coverView.backgroundColor = [UIColor clearColor];
[window addSubview:coverView];
UIView *bgView = [[UIView alloc] init];
bgView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
bgView.bounds = CGRectMake(0, 0, msgSize.width+30, msgSize.height+30);
bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
bgView.layer.cornerRadius = 4;
bgView.layer.masksToBounds = YES;
[coverView addSubview:bgView];
UILabel *alertLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, bgView.frame.size.width-30, bgView.frame.size.height-30)];
alertLab.text = message;
alertLab.textAlignment = NSTextAlignmentCenter;
alertLab.textColor = [UIColor whiteColor];
alertLab.font = [UIFont systemFontOfSize:12];
alertLab.numberOfLines = 0;
[bgView addSubview:alertLab];
[coverView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
}