iOS Objective-C 在 UIAlertView 中换行 UITextField 的文本并根据内容动态调整高度 [英] iOS Objective-C wrap text of UITextField in UIAlertView and dynamically adjust height based on content
问题描述
我有一个 UIAlertView
和 UIAlertViewStylePlainTextInput
的 alertViewStyle
.我试图找到一种方法,使 UIAlertView
的 UITextField
调整其高度并根据用户键入的内容包装其文本.
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.
这是我的一些代码:
-(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];
}
推荐答案
UITextField
不可能有多行和动态高度.
It is not possible to have a UITextField
with multiple lines and dynamic height.
要实现这一点,请创建一个自定义警报并添加一个 UITextView
,因为 UITextView
可以有多行.覆盖 textViewDidChange: 并计算 textview 中文本的大小sizeWithFont:constrainedToSize:lineBreakMode:.它将根据高度返回 CGSize
调整 UITextView
和警报的高度.
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.
谢谢
这篇关于iOS Objective-C 在 UIAlertView 中换行 UITextField 的文本并根据内容动态调整高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!