// // CBToast.m // ESVideoPhoneSDKDemo // // Created by 陈嘉乐 on 2020/6/10. // Copyright © 2020 eTouchSky. All rights reserved. // #import "CBToast.h" @implementation CBToast static UILabel *toastLabel = nil; + (UILabel *)currentToastLabel { @synchronized(self) { if (toastLabel == nil) { toastLabel = [[UILabel alloc] init]; toastLabel.backgroundColor = [UIColor darkGrayColor]; toastLabel.font = [UIFont systemFontOfSize:16]; toastLabel.textColor = [UIColor whiteColor]; toastLabel.numberOfLines = 0; toastLabel.textAlignment = NSTextAlignmentCenter; toastLabel.lineBreakMode = NSLineBreakByCharWrapping; toastLabel.layer.masksToBounds = YES; toastLabel.layer.cornerRadius = 5.0; toastLabel.alpha = 0; } return toastLabel; } } //默认显示消息-->center + (void)showToastAction:(NSString *)message { [self showToast:message location:@"center" showTime:2.0]; } //显示消息 + (void)showToast:(NSString *)message location:(NSString *)aLocationStr showTime:(float)aShowTime { if (!message) { message = @""; } if ([[NSThread currentThread] isMainThread]) { toastLabel = [self currentToastLabel]; [toastLabel removeFromSuperview]; UIWindow *window = [UIApplication sharedApplication].delegate.window; [window addSubview:toastLabel]; CGFloat main_width = [[UIScreen mainScreen] bounds].size.width; CGFloat main_height = [[UIScreen mainScreen] bounds].size.height; CGFloat width = [self stringText:message font:16 isHeightFixed:YES fixedValue:40]; CGFloat height = 0; if (width > main_width - 20) { width = main_width - 20; height = [self stringText:message font:16 isHeightFixed:NO fixedValue:width]; }else{ height = 40; } CGRect labFrame; if (aLocationStr && [aLocationStr isEqualToString:@"top"]) { labFrame = CGRectMake((main_width-width)/2, main_height*0.15, width, height); }else if (aLocationStr && [aLocationStr isEqualToString:@"bottom"]) { labFrame = CGRectMake((main_width-width)/2, main_height*0.85, width, height); }else{ //default-->center labFrame = CGRectMake((main_width-width)/2, main_height*0.5, width, height); } toastLabel.frame = labFrame; toastLabel.text = message; toastLabel.alpha = 1; [UIView animateWithDuration:aShowTime animations:^{ toastLabel.alpha = 0; } completion:^(BOOL finished) { }]; }else{ dispatch_async(dispatch_get_main_queue(), ^{ [self showToast:message location:aLocationStr showTime:aShowTime]; }); return; } } //根据字符串长度获取对应的宽度或者高度 + (CGFloat)stringText:(NSString *)aText font:(CGFloat)aFont isHeightFixed:(BOOL)isHeightFixed fixedValue:(CGFloat)fixedValue { CGSize size; if (isHeightFixed) { size = CGSizeMake(MAXFLOAT, fixedValue); } else { size = CGSizeMake(fixedValue, MAXFLOAT); } CGSize resultSize = CGSizeZero; if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) { //返回计算出的size resultSize = [aText boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:aFont]} context:nil].size; } if (isHeightFixed) { return resultSize.width + 20; //增加左右20间隔 } else { return resultSize.height + 20; //增加上下20间隔 } } @end