美文网首页
UIAlertController的标题、内容的字体和颜色

UIAlertController的标题、内容的字体和颜色

作者: 来敲代码 | 来源:发表于2017-08-10 10:02 被阅读3次
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib..
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *alertBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    alertBtn.backgroundColor = [UIColor redColor];
    [alertBtn addTarget:self action:@selector(AlertBtnAtion) forControlEvents:UIControlEventTouchUpInside];
    alertBtn.frame = CGRectMake(100, 100, 100, 100);
    
    [self.view addSubview:alertBtn];
    
    
}
- (void)AlertBtnAtion
{
    UIAlertController *AlertController = [UIAlertController alertControllerWithTitle:@"我有这么多的女朋友 今晚去谁家?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *Action1 = [UIAlertAction actionWithTitle:@"去小兰家!" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [AlertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [AlertController addAction:Action1];
    
    UIAlertAction *Action2 = [UIAlertAction actionWithTitle:@"去小红家!" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [AlertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [AlertController addAction:Action2];
    UIAlertAction *Action3 = [UIAlertAction actionWithTitle:@"去小花家!" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [AlertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [AlertController addAction:Action3];
    
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];
    [defaultAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];
    
    
    [AlertController addAction:defaultAction];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"都不见我,还是回家吧!"
                             
                                                     style:UIAlertActionStyleCancel
                             
                                                   handler:^(UIAlertAction* action)
                             
    {
        
        [AlertController dismissViewControllerAnimated: YES completion: nil];
        
    }];
    [AlertController addAction:cancel];

    //修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [AlertController setValue:alertControllerStr forKey:@"attributedTitle"];
    
    //修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
    [AlertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    
    [self presentViewController:AlertController animated:YES completion:nil];
}
@end

相关文章

网友评论

      本文标题:UIAlertController的标题、内容的字体和颜色

      本文链接:https://www.haomeiwen.com/subject/bwncrxtx.html