3

Color change in attributedMessage and attributedTitle doesn't work. Is there a solution for it? Works well on iOS 12 but no longer works on iOS 13. What can be done or is there a solution or a modification?

Here's the full snippet:

NSString *title=NSLocalizedString(@"Title",nil);
NSString *message=NSLocalizedString(@"Message",nil);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:({
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Other1",nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action0) {
        NSLog(@"OK1");


    }];
    [action0 setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
    action0;
})];
[alertController addAction:({
    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"Other2",nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"OK2");


    }];
    [action setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
    action;
})];
[alertController addAction:({
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action2) {

    }];
    [action2 setValue:[UIColor orangeColor] forKey:@"titleTextColor"];
    action2;
})];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
NSMutableAttributedString *titleText;
titleText = [[NSMutableAttributedString alloc]
             initWithString:title
             attributes:@{NSParagraphStyleAttributeName: paragraphStyle,
                          NSFontAttributeName : [UIFont boldSystemFontOfSize:17],
                          NSForegroundColorAttributeName :[UIColor whiteColor]
                          }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setAlignment:NSTextAlignmentCenter];
NSMutableAttributedString *messageText;
messageText = [[NSMutableAttributedString alloc]
               initWithString:message
               attributes:@{NSParagraphStyleAttributeName: paragraphStyle2,
                            NSFontAttributeName : [UIFont boldSystemFontOfSize:14],
                            NSForegroundColorAttributeName : [UIColor lightTextColor]
                            }];

[alertController setValue:titleText  forKey:@"attributedTitle"];
[alertController setValue:messageText forKey:@"attributedMessage"];


[self presentViewController:alertController animated:YES completion:nil];
  • 1
    I just tested your code and it seems to be working fine. I even changed the colors is this is the result: https://imgur.com/8n3ZGQt How are you setting the title and message strings? – Marina Aguilar Oct 03 '19 at 18:44
  • I put the full code below – Albert Parti Oct 03 '19 at 19:20
  • I see you're using UIAlertControllerStyleActionSheet. I found some things that might change attributed text for this type of uialertcontroller presentation, but they are private APIs and your app can be rejected for using it. Aparently there is no legal way to change those labels with the public apis. – Marina Aguilar Oct 03 '19 at 19:59
  • UIAlertController doesn't support what you are doing. It's a bad idea to poke into the private values of the class. Write or find a custom alert class that supports what you need. – rmaddy Oct 03 '19 at 20:06
  • That's a HACK. It's never a good idea to use private API as they might change without notice. – Larme Oct 04 '19 at 09:10

0 Answers0