封装好的可设置标题、正文字体的默认样式UIAlertController

来源:互联网 发布:软件验收测试 编辑:程序博客网 时间:2024/05/21 22:41

iOS8之后,主要使用UIAlertController进行信息的显示,而默认的样式使用的比较多,所以封装了一个,以方便以后使用。

这里写图片描述

调用以下函数即可:

-(void)showIntroDuction:(NSString *)title :(NSString *)showText :(NSString *)btnStr :(UIFont *)titleFont :(UIFont *) showTextFont :(UIColor *) titleColor :(UIColor *)showTextColor :(UIColor *)btnColor{    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:showText preferredStyle:UIAlertControllerStyleAlert];    // 设置标题的字体    NSMutableAttributedString *attributedStrTitle = [[NSMutableAttributedString alloc]initWithString:title];    [attributedStrTitle addAttribute:NSFontAttributeName value:titleFont range:NSMakeRange(0, [title length])];    [attributedStrTitle addAttribute:NSForegroundColorAttributeName value:titleColor range:NSMakeRange(0, [title length])];    // 利用KVC赋值    [alertController setValue:attributedStrTitle forKey:@"attributedTitle"];    // 设置正文的字体    NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:showText];    [messageAtt addAttribute:NSFontAttributeName value:showTextFont range:NSMakeRange(0, [showText length])];    [messageAtt addAttribute:NSForegroundColorAttributeName value:showTextColor range:NSMakeRange(0, [showText length])];    [alertController setValue:messageAtt forKey:@"attributedMessage"];    // 按钮    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:btnStr style:UIAlertActionStyleDefault handler:nil];    [alertController addAction:alertAction];    // 按钮颜色    [alertAction setValue:btnColor forKey:@"_titleTextColor"];    [self presentViewController:alertController animated:YES completion:nil];}