.h文件
#import <UIKit/UIKit.h>
@interface Toast : UIView
/*!
@brief 初始化
*/
+(Toast *)makeText:(NSString *)text;
/*!
@brief 显示出来
*/
-(void)show;
@end
.m文件
#import "Toast.h"
@interface Toast()
@property(nonatomic,strong)UILabel * titleLabel;
@end
@implementation Toast
+(Toast *)makeText:(NSString *)text
{
static Toast * toast=nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
toast=[[self alloc]init];
toast.titleLabel=[[UILabel alloc]init];
toast.titleLabel.textColor=[UIColor whiteColor];
toast.titleLabel.textAlignment=NSTextAlignmentCenter;
toast.titleLabel.layer.masksToBounds=YES;
toast.titleLabel.numberOfLines=0;
toast.titleLabel.layer.cornerRadius=15*SCALE;
toast.titleLabel.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.75f];
});
// if(text.length>15)
// {
// text=[NSString stringWithFormat:@"%@...",[text substringToIndex:12]];
// }
CGSize size=[text boundingRectWithSize:CGSizeMake(250*SCALE, 100*SCALE) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20*SCALE]} context:nil].size;
toast.titleLabel.frame=CGRectMake((SCREENWIDTH-size.width-30*SCALE)/2, SCREENHEIGHT-50*SCALE-size.height, size.width+30*SCALE, size.height+10*SCALE);
toast.titleLabel.text=text;
return toast;
}
-(void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self.titleLabel];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.titleLabel];
[UIView animateWithDuration:3.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.titleLabel.alpha=0;
} completion:^(BOOL finished) {
self.titleLabel.alpha=1;
[self.titleLabel removeFromSuperview];
}];
}
@end
[[Toast makeText:@"要显示内容"]] show];