1 2 3
| // 对外暴露两个方法供调用 + (void)showToast:(NSString *)text; + (void)showToast:(NSString *)text inView:(UIView *)superView;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| // 实现方法 + (void)showToast:(NSString *)text{ [ToastUtil showToast:text inView:[UIApplication sharedApplication].windows.lastObject]; }
+ (void)showToast:(NSString *)text inView:(UIView *)superView { if (!superView) { return; } CGSize labelSize = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.f]}]; UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:18.f]; label.text = text; label.textAlignment = NSTextAlignmentCenter; label.layer.cornerRadius = labelSize.height/4; label.layer.masksToBounds = YES; label.backgroundColor = [UIColor colorWithRed:38/255.f green:187/255.f blue:251/255.f alpha:1.f]; label.textColor = [UIColor whiteColor]; label.frame = CGRectMake((superView.bounds.size.width - labelSize.width)/2, 0, labelSize.width, labelSize.height); [superView addSubview:label]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [label removeFromSuperview]; }); }
|
-------------本文结束感谢您的阅读-------------