IOS AlertController with multi-line UITextField

I am working on an application, but it is quite old. The client doesn't want to spend too many hours, so I'm looking for an easy and quick fix for this problem.

Users have to write a summary for their profile, so they need a lot of recording space. The whole application was built with AlertController flyouts, so I would like to replace one line of the UITextField with a larger multi-line UITextField.

What can I do for this? I have currently 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];
    }];

      

This means that the UITextField is actually larger, but it is not multi-line, and it also centers the text vertically. What can I do about this?

+3


source to share


2 answers


As @rmaddy said, UITextField

is only for one line line, you can use UITextView

.

You can accomplish it like 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 embed 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 create your own alert like ios-custom-alertview

+2


source


You can embed the ViewController in 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:^{

    }];
}

      

+1


source







All Articles