当在一个多次要用到弹窗的时候,我们会觉得很麻烦每次都重写。于是自己创建一个单独的类来重写这个关于弹窗的方法,再用到的时候我们只需要倒入头文件,调用这个方法即可,比较方便。下面是一个小方法。
#import "Alert.h"
@implementation Alert
+(void)alert:(NSString *)str andUIviewController:(UIViewController *)v completion:(void (^ ) (void))completion{
UIAlertController *a =[UIAlertController alertControllerWithTitle:@"友情提示" message:str preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ac =[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[a addAction:ac];
[v presentViewController:a animated:YES completion:completion];
}
@end
修改title和message的字体颜色和大小
//修改title
NSString* titleStr =@"提示";
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:titleStr];
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, titleStr.lenth)];
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, titleStr.lenth)];
if (alertController valueForKey:@"attributedTitle") {
[alertController setValue:alertControllerStr forKey:@"attributedTitle"];
}
//修改message
NSString* messegeStr =@"提示内容";
NSMutableAttributedString *alertControllerMessageStr =
[[NSMutableAttributedString alloc] initWithString:messegeStr];
[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, messegeStr.lenth)];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, messegeStr.lenth)];
if (alertController valueForKey:@"attributedMessage") {
[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
}