1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
//  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