當在一個多次要用到彈窗的時候愿险,我們會覺得很麻煩每次都重寫笛丙。于是自己創(chuàng)建一個單獨的類來重寫這個關于彈窗的方法漾脂,再用到的時候我們只需要倒入頭文件,調(diào)用這個方法即可胚鸯,比較方便骨稿。下面是一個小方法。
#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 =@"提示內(nèi)容";
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"];
}