IOS 仿Android吐司提示框的实例(分享)
程序员文章站
2023-12-19 17:12:22
直接上代码
#import
@interface showtoastview : uiview
+(void)sh...
直接上代码
#import <uikit/uikit.h> @interface showtoastview : uiview +(void)showtoastview:(uiview *)uiview withmessage:(nsstring *)message; +(void)showtoastviewshort:(uiview *)uiview withmessage:(nsstring *)message; +(void)showtoastviewwithcostupload:(uiview *)uiview withmessage:(nsstring *)message; +(void)showsmallheighttoastview:(uiview *)uiview withmessage:(nsstring *)message; @end
#import "showtoastview.h" @implementation showtoastview //toast提示框 +(void)showtoastview:(uiview *)uiview withmessage:(nsstring *)message { uiview *showview = [[uiview alloc]init]; showview.backgroundcolor = [uicolor colorwithred:0 green:0 blue:0 alpha:.3]; showview.frame = cgrectmake(1, 1, 1, 1); showview.layer.cornerradius = 5.0f; showview.layer.maskstobounds = yes; [uiview addsubview:showview]; uilabel *label = [[uilabel alloc]init]; cgsize labelsize = [message sizewithfont:[uifont systemfontofsize:17] constrainedtosize:cgsizemake(290, 9000)]; label.frame = cgrectmake(10, 5, labelsize.width, labelsize.height); label.text = message; label.textcolor = [uicolor whitecolor]; label.textalignment = 1; label.backgroundcolor = [uicolor clearcolor]; label.font = [uifont boldsystemfontofsize:font(15)]; [showview addsubview:label]; showview.frame = cgrectmake((uiview.frame.size.width - labelsize.width - 20)/2, uiview.frame.size.height - labelsize.height-100, labelsize.width+20, labelsize.height+10); [uiview animatewithduration:5.0 animations:^{ showview.alpha = 0; } completion:^(bool finished) { [showview removefromsuperview]; }]; } +(void)showtoastviewshort:(uiview *)uiview withmessage:(nsstring *)message { uiview *showview = [[uiview alloc]init]; showview.backgroundcolor = [uicolor whitecolor]; showview.frame = cgrectmake(1, 1, 1, 1); showview.layer.cornerradius = 5.0f; showview.layer.maskstobounds = yes; [uiview addsubview:showview]; uilabel *label = [[uilabel alloc]init]; cgsize labelsize = [message sizewithfont:[uifont systemfontofsize:17] constrainedtosize:cgsizemake(290, 9000)]; label.frame = cgrectmake(10, 5, labelsize.width, labelsize.height); label.text = message; label.textcolor = [uicolor blackcolor]; label.textalignment = 1; label.backgroundcolor = [uicolor clearcolor]; label.font = [uifont boldsystemfontofsize:15]; [showview addsubview:label]; showview.frame = cgrectmake((uiview.frame.size.width - labelsize.width - 20)/2, uiview.frame.size.height - labelsize.height-60, labelsize.width+20, labelsize.height+10); [uiview animatewithduration:1 animations:^{ showview.alpha = 0; } completion:^(bool finished) { [showview removefromsuperview]; }]; } //费用提报的toast位置往上放一点 +(void)showtoastviewwithcostupload:(uiview *)uiview withmessage:(nsstring *)message { uiview *showview = [[uiview alloc]init]; showview.backgroundcolor = [uicolor colorwithred:0 green:0 blue:0 alpha:.3]; showview.frame = cgrectmake(1, 1, 1, 1); showview.layer.cornerradius = 5.0f; showview.layer.maskstobounds = yes; [uiview addsubview:showview]; uilabel *label = [[uilabel alloc]init]; cgsize labelsize = [message sizewithfont:[uifont systemfontofsize:17] constrainedtosize:cgsizemake(290, 9000)]; label.frame = cgrectmake(10, 5, labelsize.width, labelsize.height); label.text = message; label.textcolor = [uicolor whitecolor]; label.textalignment = 1; label.backgroundcolor = [uicolor clearcolor]; label.font = [uifont boldsystemfontofsize:font(15)]; [showview addsubview:label]; showview.frame = cgrectmake((uiview.frame.size.width - labelsize.width - 20)/2, uiview.frame.size.height - labelsize.height-100, labelsize.width+20, labelsize.height+10); [uiview animatewithduration:3.0 animations:^{ showview.alpha = 0; } completion:^(bool finished) { [showview removefromsuperview]; }]; } //点击开始按钮的时候提示没有任务,但是由于字数太多,高度又和宽度有一定的对比,所以在这里改成小一点高度 +(void)showsmallheighttoastview:(uiview *)uiview withmessage:(nsstring *)message { uiview *showview = [[uiview alloc]init]; showview.backgroundcolor = [uicolor colorwithred:0 green:0 blue:0 alpha:.3]; showview.frame = cgrectmake(1, 1, 1, 1); showview.layer.cornerradius = 5.0f; showview.layer.maskstobounds = yes; [uiview addsubview:showview]; uilabel *label = [[uilabel alloc]init]; cgsize labelsize = [message sizewithfont:[uifont systemfontofsize:17] constrainedtosize:cgsizemake(290, 9000)]; label.frame = cgrectmake(10, 0, labelsize.width, labelsize.height); label.text = message; label.textcolor = [uicolor whitecolor]; label.textalignment = 1; label.backgroundcolor = [uicolor clearcolor]; label.font = [uifont boldsystemfontofsize:font(15)]; [showview addsubview:label]; showview.frame = cgrectmake((uiview.frame.size.width - labelsize.width - 20)/2, uiview.frame.size.height - labelsize.height-60, labelsize.width+20, labelsize.height-5); [uiview animatewithduration:5.0 animations:^{ showview.alpha = 0; } completion:^(bool finished) { [showview removefromsuperview]; }]; } @end
使用方法
[showtoastview showtoastview:self.view withmessage:@"用户名或密码错误"];
以上这篇ios 仿android吐司提示框的实例(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。