uialert objective example ios ios8 uialertcontroller

ios - objective - Alineación de texto de UIAlertController



uialertcontroller swift 4 (4)

Esta propiedad expone los campos de texto, por lo que probablemente podría usarse para configurarlos:

campos de texto

La matriz de campos de texto mostrada por la alerta. (solo lectura)

Declaración

RÁPIDO

var textFields: [AnyObject]? { get }

C OBJETIVO

@property(nonatomic, readonly) NSArray *textFields

Discusión

Use esta propiedad para acceder a los campos de texto que muestra la alerta. Los campos de texto están en el orden en que los agregó al controlador de alertas. Esta orden también corresponde al orden en que se muestran en la alerta.

Nota: aunque dice que la propiedad es de readonly , devuelve una matriz de referencias de objetos de campo de texto que se pueden usar para modificar los controles.

¿Hay alguna manera de cambiar la alineación del mensaje que se muestra dentro de un UIAlertController en iOS 8?

Creo que ya no es posible acceder a las subvistas y cambiarlas por UILabel.


Navegue hasta el árbol de la subvista hasta que llegue a UILabels para el título y el mensaje

NSArray *viewArray = [[[[[[[[[[[[alertController view] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews]; UILabel *alertTitle = viewArray[0] UILabel *alertMessage = viewArray[1]; alertMessage.textAlignment = NSTextAlignmentLeft;

Sin embargo, es posible que desee hacer una categoría para ello

@interface UIAlertController (ShowMeTheLabels) @property (nonatomic, strong) UILabel *titleLabel, *messageLabel; @end @implementation UIAlertController (ShowMeTheLabels) @dynamic titleLabel; @dynamic messageLabel; - (NSArray *)viewArray:(UIView *)root { NSLog(@"%@", root.subviews); static NSArray *_subviews = nil; _subviews = nil; for (UIView *v in root.subviews) { if (_subviews) { break; } if ([v isKindOfClass:[UILabel class]]) { _subviews = root.subviews; return _subviews; } [self viewArray:v]; } return _subviews; } - (UILabel *)titleLabel { return [self viewArray:self.view][0]; } - (UILabel *)messageLabel { return [self viewArray:self.view][1]; } @end

Entonces puedes alinear el texto como este

yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;


He utilizado con éxito lo siguiente, tanto para alinear como para diseñar el texto de UIAlertControllers:

let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.Left let messageText = NSMutableAttributedString( string: "The message you want to display", attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody), NSForegroundColorAttributeName : UIColor.blackColor() ] ) myAlert.setValue(messageText, forKey: "attributedMessage")

Puede hacer algo similar con el título, si usa "attributedTitle" , en lugar de "attributedMessage"

Swift 3 actualización

Lo anterior todavía funciona en Swift 3, pero el código tiene que ser ligeramente modificado para esto:

let messageText = NSMutableAttributedString( string: "The message you want to display", attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName : UIFont.preferredFont(forTextStyle: UIFontTextStyle.body), NSForegroundColorAttributeName : UIColor.black ] ) myAlert.setValue(messageText, forKey: "attributedMessage")


Usa el siguiente código

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];