iOS 提示框工具

//

//  ToastUtils.h

//  vsfa

//

//  Created by long on 15/7/29.

//  Copyright © 2015 long. All rights reserved.

//


#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


#define ShowToastAtTop(format, ...) \

[ToastUtils showAtTop:[NSString stringWithFormat:format, ## __VA_ARGS__]]


#define ShowToast(format, ...) \

[ToastUtils show:[NSString stringWithFormat:format, ## __VA_ARGS__]]


#define ShowToastLongAtTop(format, ...) \

[ToastUtils showLongAtTop:[NSString stringWithFormat:format, ## __VA_ARGS__]]


#define ShowToastLong(format, ...) \

[ToastUtils showLong:[NSString stringWithFormat:format, ## __VA_ARGS__]]


@interface ToastUtils : NSObject


//显示提示视图, 默认显示在屏幕上方,防止被软键盘覆盖,1.5s后自动消失

+ (void)showAtTop:(NSString *)message;


//显示提示视图, 默认显示在屏幕下方,1.5s后自动消失

+ (void)show:(NSString *)message;


//显示提示视图, 默认显示在屏幕上方,防止被软键盘覆盖,3s后自动消失

+ (void)showLongAtTop:(NSString *)message;


//显示提示视图, 默认显示在屏幕下方,3s后自动消失

+ (void)showLong:(NSString *)message;


@end

// 版权属于原作者

// http://code4app.com (cn) http://code4app.net (en)

// 发布代码于最专业的源码分享网站: Code4App.com


//

//  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

// 版权属于原作者

// http://code4app.com (cn) http://code4app.net (en)

// 发布代码于最专业的源码分享网站: Code4App.com


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值