博客> ios 修改UIAlertController的title、message、按钮颜色及大小
ios 修改UIAlertController的title、message、按钮颜色及大小
2017-09-24 22:01 评论:0 阅读:171 飞翔信念
ios message 修改UIAlertController的title 按钮颜色及大小

刚做的一个项目中对弹窗有点要求改变颜色及大小,上网搜了下,总结记录一下 参考:http://blog.csdn.net/gaomingyangc/article/details/51699416

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"老板圈" message:@"您当前还不是高级会员,不能进入老板圈子哦。快去成为高级会员吧。" preferredStyle:UIAlertControllerStyleAlert];

    //修改标题的内容,字号,颜色。使用的key值是“attributedTitle”
    NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"老板圈"];

    [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, [[hogan string] length])];

    [hogan addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [[hogan string] length])];

    [alertController setValue:hogan forKey:@"attributedTitle"];

    /*message*/
    NSMutableAttributedString *alertMessageStr = [[NSMutableAttributedString alloc] initWithString:@"\n您当前还不是高级会员,不能进入老板圈子哦。快去成为高级会员吧。"];

    [alertMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [[alertMessageStr string] length])];

    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[[CYTool sharedInstance] colorWithHexString:@"#888888"] range:NSMakeRange(0, [[alertMessageStr string] length])];

    [alertController setValue:alertMessageStr forKey:@"attributedMessage"];

    //修改按钮的颜色,同上可以使用同样的方法修改内容,样式
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"成为高级会员" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSLog(@"点击事件");

    }];

    [defaultAction setValue:[[CYTool sharedInstance] colorWithHexString:@"#2591ff"] forKey:@"_titleTextColor"];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];

    [cancelAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];

    [alertController addAction:cancelAction];

    [alertController addAction:defaultAction];

    //获取到message并设置左对齐
    UIView *subView1 = alertController.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    UIView *subView5 = subView4.subviews[0];

    UILabel *message = subView5.subviews[1];
    message.textAlignment = NSTextAlignmentLeft;

    [self presentViewController:alertController animated:YES completion:nil];
收藏
0
sina weixin mail 回到顶部