Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS AlertController with multi-line UITextField

I'm working on an app, but it's a rather old one. The client doesn't want to spend too many hours, so I'm looking for a simple and quick fix for this issue.

Users have to write a summary for their profile, so they need a big space to write in. The whole app was built with AlertController pop-ups, so I'd like to replace a single line UITextField with a larger multiline UITextField.

What could I do to accomplish that? I currently have added the following code:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"Enter your summary" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"Summary";
        textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
        textField.text = _summaryLabel.text;

        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant: 110.0];
        [textField addConstraint:constraint];
        [textField layoutIfNeeded];
    }];

That means that the UITextField is actually bigger, but it's not multiline and it's also centering the text vertically. What could I do about this?

like image 205
jbehrens94 Avatar asked Apr 24 '17 14:04

jbehrens94


2 Answers

As @rmaddy said, UITextField is only for single-line, you can use UITextView.

You can accomplish that by this:

How to use UITextView in UIAlertController

UIAlertController *alert = [UIAlertController
                            alertControllerWithTitle:@"Enter your summary"
                            message:@"\n\n\n\n\n\n\n\n"
                            preferredStyle:UIAlertControllerStyleAlert];
alert.view.autoresizesSubviews = YES;

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *leadConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-8.0];
NSLayoutConstraint *trailConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0];

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64.0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:64.0];
[alert.view addSubview:textView];
[NSLayoutConstraint activateConstraints:@[leadConstraint, trailConstraint, topConstraint, bottomConstraint]];

[alert addAction: [UIAlertAction actionWithTitle:@"Done"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {
    NSLog(@"%@", textView.text);
}]];

[self presentViewController:alert animated:YES completion:nil];

UIAlertController

Or use UIAlertView:

How to Insert the UITextView into UIAlertview in iOS

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"Enter your summary"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Done", nil];

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
[alert setValue:textView forKey:@"accessoryView"];
[alert show];

UIAlertView

Or custom your own alert like ios-custom-alertview

like image 196
tomfriwel Avatar answered Nov 09 '22 05:11

tomfriwel


You can embed a ViewController within the AlertController.

-(void)showAlertWithTitle:(NSString *)title andBody:(NSString *)body andTextToEdit:(NSString*)text{

    UIViewController *controller = [[UIViewController alloc]init];

    // Some size
    CGRect rect = CGRectMake(0, 0, 272, 250);
    [controller setPreferredContentSize:rect.size];

    // The text view to be used
    UITextView *textView = [[UITextView alloc]initWithFrame:rect];

    [textView setText:text]; // Original text is there to edit
    [controller.view addSubview:textView];  // Add the textField to the controller

    // I needed these when adding a UITableView, not sure if needed for UITextView
    [controller.view bringSubviewToFront:textView];
    [controller.view setUserInteractionEnabled:YES];

    alertWithText = [UIAlertController alertControllerWithTitle:title message:body preferredStyle:UIAlertControllerStyleAlert];
    [alertWithText setValue:controller forKey:@"contentViewController"];

    // create the actions handled by each button
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"The Edited Text : %@", textView.text);
    }];

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

    }];


        // add the actions to our alert
    [alertWithText addAction:action1];
    [alertWithText addAction:action2];

    [self presentViewController:alertWithText animated:YES completion:^{

    }];
}
like image 36
Recycled Steel Avatar answered Nov 09 '22 04:11

Recycled Steel