iOS 仿Android Toast效果

.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];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值