AlertController 使用 以及设置文本颜色
2017-09-05 本文已影响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];