利用富文本修改UIAlertController的属性

昨天由于工作需要,学习了一下富文本,今天早上先来整理一下。方便之后的查找。

  • 本来以为UIAlertController的message无法进行富文本,后来经过查找资料,并且进行测试,发现可以行的通。现将代码粘贴如下:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"协议号" message:@"学号:123456" preferredStyle:UIAlertControllerStyleAlert];
 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
 NSMutableAttributedString *alertMessageStr = [[NSMutableAttributedString alloc] initWithString:@"学号:123456"];
    [alertMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, alertMessageStr.length)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(3, 6)];
    [alertController setValue:alertMessageStr forKey:@"attributedMessage"];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];

1、修改UIAlertController的title的字体颜色、大小

/*title*/
    NSMutableAttributedString *alertTitleStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    [alertTitleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 2)];
    [alertTitleStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [alertController setValue:alertTitleStr forKey:@"attributedTitle"];

2、修改UIAlertController的message的字体颜色、字号大小

 /*message*/
    NSMutableAttributedString *alertMessageStr = [[NSMutableAttributedString alloc] initWithString:@"请修改输入内容"];
    [alertMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 7)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 2)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 2)];
    [alertController setValue:alertMessageStr forKey:@"attributedMessage"];

好了、今天就整理到这里。

你可能感兴趣的:(利用富文本修改UIAlertController的属性)