gpt4 book ai didi

ios - UITextfield 的文本在填充后不会向左移动 - 因为我一直在输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:23 27 4
gpt4 key购买 nike

我使用以下代码在 Tableview 的单元格中使用了 UITextfield:

- (UITextField *)textFieldWithDefaultsForCell:(int)cellIdentifier {
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(180, 12, 135, 20)] autorelease];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
textField.textColor = [UIColor colorWithRed:66.0/225.0 green:77.0/255.0 blue:103.0/255.0 alpha:1.0];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = cellIdentifier;
textField.placeholder = @"Enter text";

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

return textField;
}

然后,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
....
switch (indexPath.row) {
case EmailAddressCellIdentifier:
cell.textLabel.text = @"Email Address";
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.text = configurator.emailAddress;
[emailAddressField release];
emailAddressField = [textField retain];

[cell.contentView addSubview:textField];
break;


....
....

}

现在,请看下图:

iPhone-screenshot

在电子邮箱中,我输入了 abcdef-123456789...但是 View 被卡住了。它没有向右移动,我看不到文本的其余部分 (6789...)。我只能看到 abcdef-12345

你能告诉我,错误在哪里吗?

最佳答案

改变行:

UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(180, 12, 135, 20)] autorelease];

收件人:

UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(135, 12, 180, 20)] autorelease];

您的文本字段的框架宽度(CGRectMake() 中的第三个参数)太小,无法容纳文本。

顺便说一句,如果表单不是动态的,请考虑在 Storyboard中创建静态单元格而不是动态填充表格。通常,如果您的 cellForRowAtIndexPath: 方法中有一个大型 switch 语句,则静态表会调用您的名字。

编辑 1

您还可以通过以下方式创建文本字段的框架,将其 x 设置为与随附标签的一定距离:

CGRectMake(label.bounds.size.width + paddingBetweenTextFieldAndLabel, 12, 135, 20)

我们甚至可以通过根据单元格尺寸设置文本字段的高度和宽度来扩展这个概念:

CGFloat textFieldX = label.bounds.size.width + paddingBetweenTextFieldAndLabel;
CGRectMake(textFieldX,
label.frame.origin.y,
cell.bounds.size.width - (textFieldX),
cell.bounds.size.height)

如果您将文本字段的标签作为参数传递给您的方法:

- (UITextField *)textFieldWithDefaultsForCell:(int)cellIdentifier

上面的代码应该可以工作。

编辑 2

如果您担心文本显示在右侧,请设置其对齐方式:

textField.textAlignment = NSTextAlignmentRight;

关于ios - UITextfield 的文本在填充后不会向左移动 - 因为我一直在输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com