昨天由于工作需要,學習了一下富文本,今天早上先來整理一下。方便之后的查找。
- 本來以為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"];
好了、今天就整理到這里。