ToastUtils.m
3.25 KB
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
//
// ToastUtils.m
// vsfa
//
// Created by long on 15/7/29.
// Copyright © 2015年 long. All rights reserved.
//
#import "ToastUtils.h"
#import "ZLDefine.h"
@implementation ToastUtils
#pragma mark - 显示提示视图
+ (void)showAtTop:(NSString *)message
{
[self show:message atTop:YES showTime:2.0];
}
+ (void)show:(NSString *)message
{
[self show:message atTop:NO showTime:2.0];
}
+ (void)showLongAtTop:(NSString *)message
{
[self show:message atTop:YES showTime:4.0];
}
+ (void)showLong:(NSString *)message
{
[self show:message atTop:NO showTime:4.0];
}
static UILabel *toastView = nil;
+ (void)show:(NSString *)message atTop:(BOOL)atTop showTime:(float)showTime
{
if (![[NSThread currentThread] isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self show:message atTop:atTop showTime:showTime];
});
return;
}
@synchronized(self){
if (toastView == nil) {
toastView = [[UILabel alloc] init];
toastView.backgroundColor = [UIColor darkGrayColor];
toastView.textColor = [UIColor whiteColor];
toastView.font = [UIFont systemFontOfSize:17];
toastView.layer.masksToBounds = YES;
toastView.layer.cornerRadius = 3.0f;
toastView.textAlignment = NSTextAlignmentCenter;
toastView.alpha = 0;
toastView.numberOfLines = 0;
toastView.lineBreakMode = NSLineBreakByCharWrapping;
[[UIApplication sharedApplication].keyWindow addSubview:toastView];
}
}
if (toastView.superview != [UIApplication sharedApplication].keyWindow) {
[toastView removeFromSuperview];
[[UIApplication sharedApplication].keyWindow addSubview:toastView];
}
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat width = [self stringText:message font:18 isHeightFixed:YES fixedValue:30];
CGFloat height = 30;
if (width > screenWidth - 20) {
width = screenWidth - 20;
height = [self stringText:message font:18 isHeightFixed:NO fixedValue:width];
}
CGRect frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-width)/2, atTop?[UIScreen mainScreen].bounds.size.height*0.15:[UIScreen mainScreen].bounds.size.height*0.85, width, height);
toastView.alpha = 1;
toastView.text = message;
toastView.frame = frame;
[UIView animateWithDuration:showTime animations:^{
toastView.alpha = 0;
} completion:^(BOOL finished) {
}];
}
//根据字符串长度获取对应的宽度或者高度
+ (CGFloat)stringText:(NSString *)text font:(CGFloat)font isHeightFixed:(BOOL)isHeightFixed fixedValue:(CGFloat)fixedValue
{
CGSize size;
if (isHeightFixed) {
size = CGSizeMake(MAXFLOAT, fixedValue);
} else {
size = CGSizeMake(fixedValue, MAXFLOAT);
}
CGSize resultSize;
//返回计算出的size
resultSize = [text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:font]} context:nil].size;
if (isHeightFixed) {
return resultSize.width;
} else {
return resultSize.height;
}
}
@end