UIAlertController text alignment

前端 未结 8 1824
醉话见心
醉话见心 2020-12-05 04:07

Is there a way to change the alignment of the message displayed inside a UIAlertController on iOS 8?

I believe accessing the subviews and changing it for the UILabel

8条回答
  •  广开言路
    2020-12-05 04:59

    Use the below code

    UIAlertController * alert = [UIAlertController
                                     alertControllerWithTitle:[title lowercaseString]
                                     message:[message lowercaseString]
                                     preferredStyle:UIAlertControllerStyleAlert];
    
        if (alertStyle == kUIAlertStylePlainText)
        {
            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    
            }];
        }
    
        if (okTitle) {
            UIAlertAction* ok = [UIAlertAction actionWithTitle:[okTitle lowercaseString] style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {
                                                           callback(alert, action, nil);
                                                       }];
            [alert addAction:ok];
        }
    
        if (cancelTitle) {
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:[cancelTitle lowercaseString] style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * action) {
                                                               callback(alert, nil, action);
                                                           }];
            [alert addAction:cancel];
        }
    
    
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.alignment = NSTextAlignmentLeft;
    
        NSMutableAttributedString *atrStr = [[NSMutableAttributedString alloc] initWithString:[message lowercaseString] attributes:@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont systemFontOfSize:13.0]}];
    
        [alert setValue:atrStr forKey:@"attributedMessage"];
        [viewInstance presentViewController:alert animated:YES completion:nil];
    

提交回复
热议问题