问题
I have a UIAlertView
with the alertViewStyle
of UIAlertViewStylePlainTextInput
. I'm trying to find a way to make the UITextField
of the UIAlertView
adjust its height and wrap its text based on its content as the user types.
To be quite honest, I haven't the slightest clue how to do this.
Here is some of my code:
-(void)updateTicket:(id)sender
{
UIButton *sndr = (UIButton*)sender;
int index = sndr.tag;
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTag:-7];
[alert setDelegate:self];
[alert setTitle:[NSString stringWithFormat:@"Update Ticket #%i", [ticketIDs[index] intValue]]];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert textFieldAtIndex:0].placeholder = @"Note";
[alert textFieldAtIndex:0].tag = index;
[alert textFieldAtIndex:0].autocapitalizationType = UITextAutocapitalizationTypeSentences;
[alert textFieldAtIndex:0].autocorrectionType = UITextAutocorrectionTypeYes;
[alert textFieldAtIndex:0].autoresizingMask = UIViewAutoresizingFlexibleHeight;
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:@"OK"];
[alert setCancelButtonIndex:0];
[alert show];
}
回答1:
It is not possible to have a UITextField
with multiple lines and dynamic height.
To achieve this please create a custom alert and add a UITextView
as UITextView
can have multiple lines. Override the textViewDidChange: and calculate the size of the text in the textview with
sizeWithFont:constrainedToSize:lineBreakMode:. It will return the CGSize
based on the height adjust the height of the UITextView
and the alert.
Thanks
来源:https://stackoverflow.com/questions/25941515/ios-objective-c-wrap-text-of-uitextfield-in-uialertview-and-dynamically-adjust-h