iOS 修改UIAlertController的title和message属性

修改UIAlertController的title和message属性,代码如下:


/**点击屏幕的空白处时触发事件*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{

//1.创建提示框控制器

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title"message:@"message"preferredStyle:UIAlertControllerStyleActionSheet];

//2.创建事件

UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"sure"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action) {

//do something you want when fire the sure event

NSLog(@"you click the sure button");

}];

UIAlertAction*cancelAction = [UIAlertAction actionWithTitle:@"cancel"style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnull action) {

//do something you want when fire the cancel event

NSLog(@"you click the cancel button");

}];

//3.修改显示的样式

//3.1修改title文字的显示样式

NSMutableAttributedString *titleAttribute = [[NSMutableAttributedString alloc]initWithString:@"title"];

[titleAttribute addAttribute:NSForegroundColorAttributeName value:[UIColorredColor] range:NSMakeRange(0,5)];

[titleAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:26]range:NSMakeRange(0,5)];

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

//3.2修改message文字的显示样式

NSMutableAttributedString *messageAttribute = [[NSMutableAttributedString alloc]initWithString:@"message"];

[messageAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor]range:NSMakeRange(0,7)];

[messageAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20]range:NSMakeRange(0,7)];

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

//3.3修改按钮的颜色

[sureAction setValue:[UIColor colorWithRed:0.1green:0.1blue:0.1alpha:1]forKey:@"titleTextColor"];

[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

//4.给提示控制器添加事件

[alertController addAction:sureAction];

[alertController addAction:cancelAction];

//5.推出并显示提示控制器alertController

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

}

代码图:

效果图:

你可能感兴趣的:(iOS 修改UIAlertController的title和message属性)