美文网首页
AlertController 使用 以及设置文本颜色

AlertController 使用 以及设置文本颜色

作者: 目染江夏 | 来源:发表于2017-09-05 17:19 被阅读12次
    NSString *title = @"您确定要讲联系人“一个人”删除,同时删除与该联系人的所有聊天记录";
    NSString *cancelButtonTitle = @"取消";
    NSString *otherButtonTitle = @"确定";
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:title preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"确定");
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    
    //修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, [title length])];
    
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:9];
    [alertControllerStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [title length])];

//    attributedMessage  attributedTitle
// attributedTitle : 修改标题文本  attributedMessage :修改提示文本
    [alertController setValue:alertControllerStr forKey:@"attributedMessage"];
    
    [cancelAction setValue:UIColorFromRGB(0x999999) forKey:@"titleTextColor"];
    [otherAction setValue:UIColorFromRGB(0x17c35c) forKey:@"titleTextColor"];

    
    [self presentViewController:alertController animated:YES completion:nil];

相关文章

网友评论

      本文标题:AlertController 使用 以及设置文本颜色

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