iOS Objective-C wrap text of UITextField in UIAlertView and dynamically adjust height based on content

可紊 提交于 2019-12-25 03:22:21

问题


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 UITextViewcan 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!